def make_app(): global _the_app if _the_app is None: p = create_publisher() _the_app = quixote.get_wsgi_app() return _the_app
def main(socketmodule=None): if socketmodule is None: socketmodule = socket app, port = get_args() if app == 'myapp': s = socketmodule.socket() host = socketmodule.getfqdn() if port == 0: port = random.randint(8000, 9999) s.bind((host, port)) print 'Starting server on', host, port print 'The Web server URL for this would be http://%s:%d/' % (host, port) s.listen(5) print 'Entering infinite loop; hit CTRL-C to exit' while True: c, (client_host, client_port) = s.accept() print 'Got connection from', client_host, client_port handle_connection(c, client_port) elif app == 'image': imageapp.setup() p = imageapp.create_publisher() wsgi_app = quixote.get_wsgi_app() from wsgiref.simple_server import make_server host = socketmodule.getfqdn() if port == 0: port = random.randint(8000, 9999) httpd = make_server('', port, wsgi_app) print 'Starting server on', host, port print 'The Web server URL for this would be http://%s:%d/' % (host, port) try: httpd.serve_forever() finally: imageapp.teardown() elif app == 'altdemo': p = create_publisher() wsgi_app = quixote.get_wsgi_app() from wsgiref.simple_server import make_server host = socketmodule.getfqdn() if port == 0: port = random.randint(8000, 9999) p.is_thread_safe = True httpd = make_server('', port, wsgi_app) print 'Starting server on', host, port print 'The Web server URL for this would be http://%s:%d/' % (host, port) httpd.serve_forever() elif app in ('quotes', 'chat'): if port == 0: port = random.randint(8000, 9999) os.chdir(app) os.system("python2.7 %s-server %d" % (app, port))
def main(): parser = argparse.ArgumentParser() parser.add_argument( '-A', '--runApp', help = 'Application to run (image/altdemo/myapp)' ) parser.add_argument( '-p', '--portNumb', help = 'Specified port number', type=int ) args = parser.parse_args() # Handle port input (if there) if args.portNumb: port = args.portNumb else: port = random.randint(8000, 9999) # Determine what app to create if args.runApp == 'myapp': wsgi_app = make_app() elif args.runApp == 'image': imageapp.setup() p = imageapp.create_publisher() wsgi_app = quixote.get_wsgi_app() elif args.runApp == 'altdemo': p = create_publisher() wsgi_app = quixote.get_wsgi_app() elif args.runApp == 'quotes': import quotes wsgi_app = quotes.setup() elif args.runApp == 'chat': import chat wsgi_app = chat.setup() elif args.runApp == 'cookie': import cookieapp wsgi_app = cookieapp.wsgi_app else: print 'Invalid Application...' return s = socket.socket() # Create a socket object host = socket.getfqdn() # Get local machine name s.bind((host, port)) # Bind to the port print 'Starting server on', host, port print 'The Web server URL for this would be http://%s:%d/' % (host, port) s.listen(5) # Now wait for client connection. print 'Entering infinite loop; hit CTRL-C to exit' while True: # Establish connection with client. c, (client_host, client_port) = s.accept() print 'Got connection from', client_host, client_port try: handle_connection(c, port, wsgi_app) finally: imageapp.teardown()
def make_app(): global _the_app global _selected_app if _the_app is None: if _selected_app == 'image': imageapp.setup() p = imageapp.create_publisher() _the_app = quixote.get_wsgi_app() elif _selected_app == 'altdemo': p = create_publisher() _the_app = quixote.get_wsgi_app() elif _selected_app == 'myapp': _the_app = app.make_app() elif _selected_app == 'django': sys.path.append(os.path.join(os.path.dirname(__file__), 'imageappdjango')) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "imageappdjango.imageappdjango.settings-prod") _the_app = get_wsgi_application() else: raise Exception("Invalid app selected") return _the_app
def init_app(current_app): global _app_init_complete if(current_app == MY_APP): return app.make_app() elif (current_app == QUIXOTE_ALTDEMO_APP): if not _app_init_complete: p = create_publisher() _app_init_complete = True return quixote.get_wsgi_app() elif (current_app == IMAGE_APP): if not _app_init_complete: imageapp.setup() p = imageapp.create_publisher() _app_init_complete = True return quixote.get_wsgi_app() elif (current_app == CHAT_APP): return ChatApp('./chat/html') elif (current_app == QUOTES_APP): return QuotesApp('./quotes/quotes.txt', './quotes/html') elif (current_app == COOKIE_APP): return cookieapp.wsgi_app
# Accepts one of the above three strings def choose_app(app_name): global _setup_complete if(app_name == APP_MINE): return app.make_app() elif(app_name == APP_QUIXOTE_ALTDEMO): if not _setup_complete: p = create_publisher() _setup_complete = True return quixote.get_wsgi_app() elif(app_name == APP_QUIXOTE_IMAGEAPP): if not _setup_complete: imageapp.setup() p = imageapp.create_publisher() _setup_complete = True return quixote.get_wsgi_app() elif(app_name == APP_CHAT): chat_app = chat.make() return chat_app elif(app_name == APP_QUOTES): quotes_app = quotes.make()
def make_altdemo(): create_publisher() return quixote.get_wsgi_app()
def main(): """Waits for a connection, then serves a WSGI app using handle_connection""" # Create a socket object sock = socket.socket() # Get local machine name (fully qualified domain name) host = socket.getfqdn() argParser = argparse.ArgumentParser(description='Set up WSGI server') argParser.add_argument('-A', metavar='App', type=str, default=['myapp'], choices=['myapp', 'imageapp', 'altdemo', 'chat', 'quotes'], help='Select which app to run', dest='app') argParser.add_argument('-p', metavar='Port', type=int, default=-1, help='Select a port to run on', dest='p') argVals = argParser.parse_args() app = argVals.app if app == 'altdemo': ## Quixote altdemo import quixote from quixote.demo.altdemo import create_publisher p = create_publisher() wsgi_app = quixote.get_wsgi_app() ## elif app == 'imageapp': ## Image app import quixote import imageapp from imageapp import create_publisher p = create_publisher() imageapp.setup() wsgi_app = quixote.get_wsgi_app() ## elif app == 'chat': ## Chat app from chat.apps import ChatApp as make_app wsgi_app = make_app('chat/html') ## elif app == 'quotes': ## Chat app from quotes.apps import QuotesApp as make_app wsgi_app = make_app('quotes/quotes.txt', 'quotes/html') else: ## My app.py from app import make_app ## ## My app.py wsgi_app = make_app() ## # Bind to a (random) port port = argVals.p if argVals.p != -1 else random.randint(8000,9999) sock.bind((host, port)) print 'Starting server on', host, port print 'The Web server URL for this would be http://%s:%d/' % (host, port) # Now wait for client connection. sock.listen(5) print 'Entering infinite loop; hit CTRL-C to exit' # Determine which web app to serve app = argVals.app[0] while True: # Establish connection with client. conn, (client_host, client_port) = sock.accept() print 'Got connection from', client_host, client_port handle_connection(conn, port, wsgi_app)
def main(socketmodule=None): if socketmodule is None: socketmodule = socket app, port = get_args() if app == 'myapp': s = socketmodule.socket() host = socketmodule.getfqdn() if port == 0: port = random.randint(8000, 9999) s.bind((host, port)) print 'Starting server on', host, port print 'The Web server URL for this would be http://%s:%d/' % (host, port) s.listen(5) print 'Entering infinite loop; hit CTRL-C to exit' while True: c, (client_host, client_port) = s.accept() print 'Got connection from', client_host, client_port handle_connection(c, client_port) elif app == 'image': imageapp.setup() p = imageapp.create_publisher() wsgi_app = quixote.get_wsgi_app() from wsgiref.simple_server import make_server host = socketmodule.getfqdn() if port == 0: port = random.randint(8000, 9999) httpd = make_server('', port, wsgi_app) print 'Starting server on', host, port print 'The Web server URL for this would be http://%s:%d/' % (host, port) try: httpd.serve_forever() finally: imageapp.teardown() elif app == 'altdemo': p = create_publisher() wsgi_app = quixote.get_wsgi_app() from wsgiref.simple_server import make_server host = socketmodule.getfqdn() if port == 0: port = random.randint(8000, 9999) p.is_thread_safe = True httpd = make_server('', port, wsgi_app) print 'Starting server on', host, port print 'The Web server URL for this would be http://%s:%d/' % (host, port) httpd.serve_forever() elif app == 'quotes': if port == 0: port = random.randint(8000, 9999) quotes_app = QuotesApp('quotes.txt', './quoteshtml') Server(port, quotes_app).serve_forever() elif app == 'chat': if port == 0: port = random.randint(8000, 9999) chat_app = ChatApp('./chathtml') Server(port, chat_app).serve_forever() elif app == 'cookie': if port == 0: port = random.randint(8000, 9999) import cookieapp from wsgiref.simple_server import make_server the_wsgi_app = cookieapp.wsgi_app host = socket.getfqdn() # Get local machine name httpd = make_server('', port, the_wsgi_app) print "Serving at http://%s:%d/..." % (host, port,) httpd.serve_forever()
def handle_connection(conn, port, app): headers = {} headers_string = '' env={} while headers_string[-4:] != '\r\n\r\n': headers_string += conn.recv(1) initLine, headerData = headers_string.split('\r\n', 1) init_list = initLine.split(' ') requestType = init_list[0] url = urlparse(init_list[1]) path = url[2] query = url[4] headers_list = headers_string.split('\r\n')[1:] for line in headerData.split('\r\n')[:-2]: key, val = line.split(': ', 1) headers[key.lower()] = val env['REQUEST_METHOD'] = 'GET' env['PATH_INFO'] = path env['QUERY_STRING'] = query env['CONTENT_TYPE'] = 'text/html' env['CONTENT_LENGTH'] = str(0) env['SCRIPT_NAME'] = '' env['SERVER_NAME'] = socket.getfqdn() env['SERVER_PORT'] = str(port) env['wsgi.version'] = (1, 0) env['wsgi.errors'] = stderr env['wsgi.multithread'] = False env['wsgi.multiprocess'] = False env['wsgi.run_once'] = False env['wsgi.url_scheme'] = 'http' env['HTTP_COOKIE'] = headers['cookie'] if 'cookie' in headers.keys() else '' def start_response(status, headers_response): conn.send('HTTP/1.0 ') conn.send(status) conn.send('\r\n') for pair in headers_response: key, header = pair conn.send(key + ': ' + header + '\r\n') conn.send('\r\n') content='' if requestType == "POST": env['REQUEST_METHOD'] = 'POST' env['CONTENT_LENGTH'] = str(headers['content-length']) try: env['CONTENT_TYPE'] = headers['content-type'] except: pass print "CONTENTLENGTH!!!" print env['CONTENT_LENGTH'] content_length = int(headers['content-length']) # !!! On Arctic, this drops data # content = conn.recv(content_length) while len(content) < content_length: content += conn.recv(1) env['wsgi.input'] = StringIO(content) if app == 'altdemo': import quixote from quixote.demo.altdemo import create_publisher try: p = create_publisher() except RuntimeError: pass wsgi = quixote.get_wsgi_app() elif app == 'image': import quixote import imageapp from imageapp import create_publisher try: p = create_publisher() imageapp.setup() except RuntimeError: pass wsgi = quixote.get_wsgi_app() elif app == "quotes": import quotes wsgi = quotes.get_wsgi_app('./quotes/quotes.txt', './quotes/html') elif app == "chat": import chat wsgi = chat.get_wsgi_app('./chat/html') elif app == "cookie": import cookieapp wsgi = cookieapp.wsgi_app else: from app import make_app wsgi = make_app() wsgi = validator(wsgi) result = wsgi(env, start_response) for data in result: conn.send(data) result.close() conn.close()
parser.add_argument('--quixote', help='Run quixote.demo.altdemo WSGI app', action = "store_true", required = False) parser.add_argument('--myapp', help='Run myapp WSGI app', action = "store_true", required = False) parser.add_argument('-A', metavar='App', type=str, nargs=1, default=[""], \ choices=['quixote','myapp', 'imageapp','quotes', 'chat', 'cookie'], help="Select which app to run ['quixote','myapp', 'imageapp','quotes', 'chat']", dest='app') args = parser.parse_args() #Check arguments if args.app == [""]: print "Please choose an app. --help to see choises" sys.exit(0) if args.quixote or args.app[0] == "quixote": from quixote.demo.altdemo import create_publisher p = create_publisher() if args.imageapp or args.app[0] == "imageapp": imageapp.setup() p = imageapp.create_publisher() def handle_connection(conn,host,port): received = conn.recv(1) if not received: print 'Error, remote client closed connection without sending anything' return while received[-4:] != '\r\n\r\n': received += conn.recv(1)