def __call__(self): return WSGIServerHttpProtocol( self.handler, readpayload=True, loop=self._loop, logger=self.worker.log, debug=self.worker.log.loglevel == logging.DEBUG, keep_alive=self.worker.cfg.keepalive, access_log=self.worker.log.access_log, access_log_format=self.access_log_format)
def factory(self, wsgi, host, port): proto = WSGIServerHttpProtocol( wsgi, loop=self.loop, log=self.log, access_log=self.log.access_log, access_log_format=self.cfg.access_log_format, readpayload=True) return self.wrap_protocol(proto)
def factory(self, wsgi, host, port): proto = WSGIServerHttpProtocol( wsgi, loop=self.loop, log=self.log, debug=self.cfg.debug, keep_alive=self.cfg.keepalive, access_log=self.log.access_log, access_log_format=self.cfg.access_log_format) return self.wrap_protocol(proto)
def factory(self, wsgi, addr): proto = WSGIServerHttpProtocol( wsgi, readpayload=True, loop=self.loop, log=self.log, debug=self.cfg.debug, keep_alive=self.cfg.keepalive, access_log=self.log.access_log, access_log_format=self.cfg.access_log_format) return self.wrap_protocol(proto)
def run(self, handler): def wsgi_app(env, start): return handler(env, start) f = self.loop.create_server( lambda: WSGIServerHttpProtocol(wsgi_app, loop=self.loop, keep_alive=60, readpayload=True, access_log=None), self.host, self.port) self._task = asyncio.Task(f, loop=self.loop) self._task.add_done_callback(self._create_server_done)
def factory(self, wsgi, addr): # are we in debug level is_debug = self.log.loglevel == logging.DEBUG proto = WSGIServerHttpProtocol( wsgi, readpayload=True, loop=self.loop, log=self.log, debug=is_debug, keep_alive=self.cfg.keepalive, access_log=self.log.access_log, access_log_format=self.cfg.access_log_format) return self.wrap_protocol(proto)
def make_server(host, port, app=None, threaded=False, processes=1, request_handler=None, passthrough_errors=False, ssl_context=None, loop=None): if threaded or processes > 1: raise ValueError("Multi-thread or process servers not supported.") loop = loop or asyncio.get_event_loop() asyncio. async (asyncio.get_event_loop().create_server( lambda: WSGIServerHttpProtocol(app, readpayload=True), host, port), loop=loop)
def run(self, handler): import asyncio from aiohttp.wsgi import WSGIServerHttpProtocol loop = asyncio.get_event_loop() loop.run_until_complete( loop.create_server( lambda: WSGIServerHttpProtocol(handler, readpayload=True, debug=self.debug, is_ssl=self.ssl), self.host, self.port)) try: loop.run_forever() except KeyboardInterrupt: loop.stop()
def make_server(host, port, app=None, threaded=False, processes=1, request_handler=None, passthrough_errors=False, ssl_context=None, loop=None): """Create a new server instance that is either threaded, or forks or just processes one request after another. """ if threaded or processes > 1: raise ValueError("Multi-thread or process servers not supported.") asyncio.set_event_loop(loop) asyncio. async (loop.create_server( lambda: WSGIServerHttpProtocol(app, readpayload=True), host, port)) loop.run_forever()
def run(addr, port, wsgi_handler, loop=None, stop=None, **options): """ Alternate version of django.core.servers.basehttp.run running on asyncio. """ if loop is None: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # The code that reads environ['wsgi.input'] is deep inside Django and hard # to make asynchronous. Pre-loading the payload is the simplest option. protocol_factory = lambda: WSGIServerHttpProtocol(wsgi_handler, readpayload=True) server = loop.run_until_complete( loop.create_server(protocol_factory, addr, port)) try: if stop is None: loop.run_forever() else: loop.run_until_complete(stop) finally: server.close() loop.run_until_complete(server.wait_closed())
def run(self, handler): def wsgi_app(env, start): def start_response(status_line, headerlist, exc_info=None): status_code = status_line.split(' ', 1)[0] headerdict = dict( map(lambda x: (x[0].lower(), x[1]), headerlist)) length = headerdict.get('content-length', 0) logger.error('{} {} {} {}'.format(env['REQUEST_METHOD'], env['RAW_URI'], status_code, length)) return start(status_line, headerlist, exc_info) return handler(env, start_response) loop = asyncio.get_event_loop() f = loop.create_server( lambda: WSGIServerHttpProtocol( wsgi_app, loop=loop, readpayload=True), self.host, self.port) loop.run_until_complete(f) try: loop.run_forever() except KeyboardInterrupt: pass
def handle_request(self, request: aiohttp.web.Request) -> \ aiohttp.web.StreamResponse: """Handle WSGI request with aiohttp""" # Use aiohttp's WSGI implementation protocol = WSGIServerHttpProtocol(request.app, True) protocol.transport = request.transport # Build WSGI Response environ = protocol.create_wsgi_environ(request, request.content) # Create responses ws = aiohttp.web.WebSocketResponse() response = aiohttp.web.StreamResponse() #: Write delegate @asyncio.coroutine def write(data): yield from response.write(data) #: EOF Write delegate @asyncio.coroutine def write_eof(): yield from response.write_eof() # WSGI start_response function def start_response(status, headers, exc_info=None): if exc_info: raise exc_info[1] status_parts = status.split(' ', 1) status = int(status_parts.pop(0)) reason = status_parts[0] if status_parts else None response.set_status(status, reason=reason) for name, value in headers: response.headers[name] = value response.start(request) return write if is_websocket_request(request): ws.start(request) # WSGI HTTP responses in websocket are meaningless. def start_response(status, headers, exc_info=None): if exc_info: raise exc_info[1] ws.start(request) return [] @asyncio.coroutine def write(data): return @asyncio.coroutine def write_eof(): return response = ws else: ws = None # Add websocket response to WSGI environment environ['wsgi.websocket'] = ws # Run WSGI app response_iter = self.wsgi(environ, start_response) try: iterator = iter(response_iter) wsgi_response = [] try: item = next(iterator) except StopIteration as stop: try: iterator = iter(stop.value) except TypeError: pass else: wsgi_response = iterator else: if isinstance(item, bytes): # This is plain WSGI response iterator wsgi_response = itertools.chain([item], iterator) else: # This is coroutine yield item wsgi_response = yield from iterator for item in wsgi_response: yield from write(item) yield from write_eof() finally: if hasattr(response_iter, 'close'): response_iter.close() # Return selected response return response