Event-driven framework (other than Twisted)?
James Mills
prologic at shortcircuit.net.au
Thu Oct 2 06:58:10 EDT 2008
On 10/2/08, Phillip B Oldham <phillip.oldham at gmail.com> wrote:
> On Oct 2, 1:32 am, "James Mills" <prolo... at shortcircuit.net.au> wrote:
> > http://hg.shortcircuit.net.au/index.wsgi/pymills/file/b7498cd4c6a4/ex...
>
> Thanks for the example, but its not loading.
Sorry my VPS has been down for quite
some time today :/ Not happy!
Here is the sample:
<code>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set sw=3 sts=3 ts=3
import optparse
from pymills import event
from pymills.event import manager
from pymills.net.sockets import TCPServer
from pymills.event.core import listener, Component
from pymills.net.http import HTTP, Response, Dispatcher
from pymills import __version__ as systemVersion
USAGE = "%prog [options] [path]"
VERSION = "%prog v" + systemVersion
###
### Functions
###
def parse_options():
"""parse_options() -> opts, args
Parse any command-line options given returning both
the parsed options and arguments.
"""
parser = optparse.OptionParser(usage=USAGE, version=VERSION)
parser.add_option("-b", "--bind",
action="store", default="0.0.0.0:8000", dest="bind",
help="Bind to address:port")
opts, args = parser.parse_args()
return opts, args
###
### Components
###
class Test(Component):
channel = "/"
@listener("index")
def onINDEX(self, request, response):
self.send(Response(response), "response")
@listener("hello")
def onHello(self, request, response):
if request.cookie.get("seen", False):
response.body = "Seen you before!"
else:
response.body = "Hello World!"
response.cookie["seen"] = True
self.send(Response(response), "response")
@listener("test")
def onTEST(self, request, response):
response.body = "OK"
self.send(Response(response), "response")
class WebServer(TCPServer, HTTP): pass
###
### Main
###
def main():
opts, args = parse_options()
if ":" in opts.bind:
address, port = opts.bind.split(":")
port = int(port)
else:
address, port = opts.bind, 80
server = WebServer(port, address)
dispatcher = Dispatcher()
event.manager += server
event.manager += Test()
event.manager += dispatcher
while True:
try:
manager.flush()
server.poll()
except KeyboardInterrupt:
break
###
### Entry Point
###
if __name__ == "__main__":
main()
</code>
--
--
-- "Problems are solved by method"
More information about the Python-list
mailing list