KamaeliaNuts & Bolts | Components | Tools | Cookbook | Systems
wiki:( guest720891, Dev Console, Index, Recent, Edit )
Some initial points:
#!/usr/bin/python# Import socket to get at constants for socketOptionsimport socket# Import the server framework, the HTTP protocol handling, the minimal request handler, and error handlersfrom Kamaelia.Chassis.ConnectedServer import SimpleServer# Our configuration
from Kamaelia.Protocol.HTTP.HTTPServer import HTTPServer
from Kamaelia.Protocol.HTTP.Handlers.Minimal import Minimal
import Kamaelia.Protocol.HTTP.ErrorPages as ErrorPageshomedirectory = "/srv/www/htdocs"# This allows for configuring the request handlers in a nicer way. This is candidate
indexfilename = "index.html"
# for merging into the mainline code. Effectively this is a factory that creates functions
# capable of choosing which request handler to use.def requestHandlers(URLHandlers):# This factory allows us to configure the minimal request handler.
def createRequestHandler(request):
if request.get("bad"):
return ErrorPages.websiteErrorPage(400, request.get("errormsg",""))
else:
for (prefix, handler) in URLHandlers:
if request["raw-uri"][:len(prefix)] == prefix:
request["uri-prefix-trigger"] = prefix
request["uri-suffix"] = request["raw-uri"][len(prefix):]
return handler(request)
return ErrorPages.websiteErrorPage(404, "No resource handlers could be found for the requested URL")
return createRequestHandlerdef servePage(request):
return Minimal(request=request,
homedirectory=homedirectory,
indexfilename=indexfilename)
# A factory to create configured HTTPServer components - ie HTTP Protocol handling componentsdef HTTPProtocol():# Finally we create the actual server and run it.
return HTTPServer(requestHandlers([
["/", servePage ],
]))SimpleServer(protocol=HTTPProtocol,
port=8082,
socketOptions=(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ).run()
So, that's your handler. To integrate this into our example from above:class HelloHandler(Axon.Component.component):
def __init__(self, request):
super(HelloHandler, self).__init__()
self.request = request
def main(self):
resource = {
"type" : "text/html",
"statuscode" : "200",
}
self.send(resource, "outbox"); yield 1
page = {
"data" : "<html><body><h1>Hello World</h1><P>Woo!!</body></html>",
}
self.send(page, "outbox"); yield 1
self.send(Axon.Ipc.producerFinished(self), "signal")
yield 1
As you can see we added in the code as expected, and added in the handler into the method at the end.#!/usr/bin/python# Import socket to get at constants for socketOptionsimport socket# We need to import Axon - Kamaelia's core component system - to write Kamaelia components!# Import the server framework, the HTTP protocol handling, the minimal request handler, and error handlers
import Axonfrom Kamaelia.Chassis.ConnectedServer import SimpleServer# Our configuration
from Kamaelia.Protocol.HTTP.HTTPServer import HTTPServer
from Kamaelia.Protocol.HTTP.Handlers.Minimal import Minimal
import Kamaelia.Protocol.HTTP.ErrorPages as ErrorPageshomedirectory = "/srv/www/htdocs"# This allows for configuring the request handlers in a nicer way. This is candidate
indexfilename = "index.html"
# for merging into the mainline code. Effectively this is a factory that creates functions
# capable of choosing which request handler to use.def requestHandlers(URLHandlers):
def createRequestHandler(request):
if request.get("bad"):
return ErrorPages.websiteErrorPage(400, request.get("errormsg",""))
else:
for (prefix, handler) in URLHandlers:
if request["raw-uri"][:len(prefix)] == prefix:
request["uri-prefix-trigger"] = prefix
request["uri-suffix"] = request["raw-uri"][len(prefix):]
return handler(request)
return ErrorPages.websiteErrorPage(404, "No resource handlers could be found for the requested URL")
return createRequestHandlerclass HelloHandler(Axon.Component.component):
def __init__(self, request):
super(HelloHandler, self).__init__()
self.request = request
def main(self):
resource = {
"type" : "text/html",
"statuscode" : "200",
}
self.send(resource, "outbox"); yield 1
page = {
"data" : "<html><body><h1>Hello World</h1><P>Woo!!</body></html>",
}
self.send(page, "outbox"); yield 1
self.send(Axon.Ipc.producerFinished(self), "signal")
yield 1
def servePage(request):
return Minimal(request=request,
homedirectory=homedirectory,
indexfilename=indexfilename)
# A factory to create configured HTTPServer components - ie HTTP Protocol handling componentsdef HTTPProtocol():# Finally we create the actual server and run it.
return HTTPServer(requestHandlers([
["/hello", HelloHandler ],
["/", servePage ],
]))SimpleServer(protocol=HTTPProtocol,
port=8082,
socketOptions=(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ).run()
One that takes the first value it sees, and stuffs it into an HTML response and sends that out its outbox.class Cat(Axon.Component.component):
def __init__(self, *args):
super(Cat, self).__init__()
self.args = args
def main(self):
self.send(self.args, "outbox")
self.send(Axon.Ipc.producerFinished(self), "signal")
yield 1
Given these two components, which can be reused to your hearts content, we can produce a simple "Echo" handler as follows:class ExampleWrapper(Axon.Component.component):
def main(self):
# Tell the browser the type of data we're sending!
resource = {
"type" : "text/html",
"statuscode" : "200",
}
self.send(resource, "outbox"); yield 1
# Send the header
header = {
"data" : "<html><body>"
}
self.send(header, "outbox"); yield 1
# Wait for it....
while not self.dataReady("inbox"):
self.pause()
yield 1
# Send the data we recieve as the page body
while self.dataReady("inbox"):
pageData = {
"data" : str(self.recv("inbox"))
}
self.send(pageData, "outbox"); yield 1
# send a footer
footer = {
"data" : "</body></html>"
}
self.send(footer, "outbox"); yield 1
# and shutdown nicely
self.send(Axon.Ipc.producerFinished(self), "signal")
yield 1
Which is actually quite sweet :-)from Kamaelia.Chassis.Pipeline import Pipeline
def EchoHandler(request):
return Pipeline ( Cat(request), ExampleWrapper() )
#!/usr/bin/python# Import socket to get at constants for socketOptionsimport socket# We need to import Axon - Kamaelia's core component system - to write Kamaelia components!# Import the server framework, the HTTP protocol handling, the minimal request handler, and error handlers
import Axonfrom Kamaelia.Chassis.ConnectedServer import SimpleServer
from Kamaelia.Protocol.HTTP.HTTPServer import HTTPServer
from Kamaelia.Protocol.HTTP.Handlers.Minimal import Minimal
import Kamaelia.Protocol.HTTP.ErrorPages as ErrorPagesfrom Kamaelia.Chassis.Pipeline import Pipeline# Our configurationhomedirectory = "/srv/www/htdocs"# This allows for configuring the request handlers in a nicer way. This is candidate
indexfilename = "index.html"
# for merging into the mainline code. Effectively this is a factory that creates functions
# capable of choosing which request handler to use.def requestHandlers(URLHandlers):
def createRequestHandler(request):
if request.get("bad"):
return ErrorPages.websiteErrorPage(400, request.get("errormsg",""))
else:
for (prefix, handler) in URLHandlers:
if request["raw-uri"][:len(prefix)] == prefix:
request["uri-prefix-trigger"] = prefix
request["uri-suffix"] = request["raw-uri"][len(prefix):]
return handler(request)
return ErrorPages.websiteErrorPage(404, "No resource handlers could be found for the requested URL")
return createRequestHandlerclass HelloHandler(Axon.Component.component):
def __init__(self, request):
super(HelloHandler, self).__init__()
self.request = request
def main(self):
resource = {
"type" : "text/html",
"statuscode" : "200",
}
self.send(resource, "outbox"); yield 1
page = {
"data" : "<html><body><h1>Hello World</h1><P>Woo!!</body></html>",
}
self.send(page, "outbox"); yield 1
self.send(Axon.Ipc.producerFinished(self), "signal")
yield 1
def servePage(request):
return Minimal(request=request,
homedirectory=homedirectory,
indexfilename=indexfilename)class Cat(Axon.Component.component):# A factory to create configured HTTPServer components - ie HTTP Protocol handling components
def __init__(self, *args):
super(Cat, self).__init__()
self.args = args
def main(self):
self.send(self.args, "outbox")
self.send(Axon.Ipc.producerFinished(self), "signal")
yield 1
class ExampleWrapper(Axon.Component.component):
def main(self):
# Tell the browser the type of data we're sending!
resource = {
"type" : "text/html",
"statuscode" : "200",
}
self.send(resource, "outbox"); yield 1
# Send the header
header = {
"data" : "<html><body>"
}
self.send(header, "outbox"); yield 1
# Wait for it....
while not self.dataReady("inbox"):
self.pause()
yield 1
# Send the data we recieve as the page body
while self.dataReady("inbox"):
pageData = {
"data" : str(self.recv("inbox"))
}
self.send(pageData, "outbox"); yield 1
# send a footer
footer = {
"data" : "</body></html>"
}
self.send(footer, "outbox"); yield 1
# and shutdown nicely
self.send(Axon.Ipc.producerFinished(self), "signal")
yield 1
def EchoHandler(request):
return Pipeline ( Cat(request), ExampleWrapper() )def HTTPProtocol():# Finally we create the actual server and run it.
return HTTPServer(requestHandlers([
["/echo", EchoHandler ],
["/hello", HelloHandler ],
["/", servePage ],
]))SimpleServer(protocol=HTTPProtocol,
port=8082,
socketOptions=(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ).run()
Versions: current , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
(C) 2005 Kamaelia Contributors, including the British Broadcasting Corporation, All Rights Reserved,
This is an ongoing community based development site. As a result the
contents of this page is the opinions of the contributors of the pages
involved not the organisations involved. Specificially, this page
may contain personal views which are not the views of the BBC.