qxserver
A script to serve a generic Quixote project as an http or scgi process.
""" qxserver.py """ from quixote2.publish import Publisher from optparse import OptionParser SCGI_PORT = 2974 HTTP_PORT = 8080 def main (): # the script supports three options and one required argument. parser = OptionParser(usage="usage: %prog [options] QuixoteDirectory") parser.set_description('Publishes the Quixote Directory object at the specified ip and port.') parser.add_option('--standalone', dest="standalone", default=False, action='store_true', help="Run as a stand alone http server (useful for development)") parser.add_option('--ip', dest="host", default='localhost', type="str", help="IP address to listen on") parser.add_option('--port', dest="port", default=SCGI_PORT, type="int", help="Port to listen on") (options, args) = parser.parse_args() # process the first argument to get the module # name and the class name. then get the class object # by importing the module and calling getattr on it. dirname = args[0] i = dirname.rfind('.') modname = dirname[:i] clsname = dirname[i+1:] m = __import__(modname, globals(), locals(), [clsname]) RootDirectory = getattr(m, clsname) # this should probably be replaced with a lambda function def create_publisher(): return Publisher(RootDirectory(), display_exceptions='plain') if options.standalone: # serve as an http process via simple_server from quixote2.server.simple_server import run run(create_publisher, host=options.host, port=options.port) else: # serve as an scgi process via scgi_server from quixote2.server.scgi_server import run run(create_publisher, host=options.host, port=options.port, script_name='') if __name__ == '__main__': main()
Usage
For these examples, assume that the mysite module exists and defines a class called MyDirectory which inherits from quixote.Directory.
- Run as an http server on port 8080
qxserver.py --standalone mysite.MyDirectory
- Run as an scgi server on port 2974
qxserver.py mysite.MyDirectory
- Run as an http server on a specific IP and port
qxserver.py --standalone --ip=192.100.10.5 --port=80 mysite.MyDirectory
- Run as a scgi server on a specific IP and port
qxserver.py --standalone --ip=127.0.0.1 --port=7777 mysite.MyDirectory
mysite.py
from quixote2 import Directory class MyDirectory (Directory): _q_exports = [''] def _q_index (self): return "Hello World"
Originally posted on: http://bitshaq.com/2010/07/20/a-simple-quixote-server/