Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise futures.TimeoutError()
loop.run_until_complete(tasks.sleep(0.001, loop=loop))
def run_once(loop):
"""Legacy API to run once through the event loop.
This is the recommended pattern for test code. It will poll the
selector once and run all callbacks scheduled in response to I/O
events.
"""
loop.call_soon(loop.stop)
loop.run_forever()
class SilentWSGIRequestHandler(WSGIRequestHandler):
def get_stderr(self):
return io.StringIO()
def log_message(self, format, *args):
pass
class SilentWSGIServer(WSGIServer):
request_timeout = 2
def get_request(self):
request, client_addr = super().get_request()
request.settimeout(self.request_timeout)
return request, client_addr
def run(port=8081, thread=False, local_path=""):
from wsgiref import simple_server
if thread:
from SocketServer import ThreadingMixIn
class myServer(ThreadingMixIn, simple_server.WSGIServer):
pass
else:
class myServer(simple_server.WSGIServer):
pass
httpd = myServer(('',port), simple_server.WSGIRequestHandler,)
if local_path:
global local_path_location
local_path_location = local_path
httpd.set_app(local_app)
else:
httpd.set_app(wsgi_app_workspace)
try:
print "Listening on port %s" % port
httpd.serve_forever()
except KeyboardInterrupt:
print "Shutting down."
hlog = logging.getLogger('mailman.http')
qlog = logging.getLogger('mailman.qrunner')
class HTTPRunner(Runner):
def __init__(self, slice=None, numslices=1):
pass
def _clean_up(self):
pass
class MailmanWSGIRequestHandler(WSGIRequestHandler):
def handle(self):
"""Handle a single HTTP request with error output to elog"""
stderr = StringIO()
saved_stderr = sys.stderr
sys.stderr = stderr
try:
WSGIRequestHandler.handle(self)
finally:
sys.stderr = saved_stderr
hlog.info(stderr.getvalue().strip())
server = make_server(config.HTTP_HOST, config.HTTP_PORT,
mailman_app,
handler_class=MailmanWSGIRequestHandler)
ws = None
if self.environ:
self.environ.pop('ws4py.socket', None)
ws = self.environ.pop('ws4py.websocket', None)
try:
SimpleHandler.finish_response(self)
except:
if ws:
ws.close(1011, reason='Something broke')
raise
else:
if ws:
self.request_handler.server.link_websocket_to_server(ws)
class WebSocketWSGIRequestHandler(WSGIRequestHandler):
WebSocketWSGIHandler = WebSocketWSGIHandler
def handle(self):
"""
Unfortunately the base class forces us
to override the whole method to actually provide our wsgi handler.
"""
self.raw_requestline = self.rfile.readline()
if not self.parse_request(): # An error code has been sent, just exit
return
# next line is where we'd have expect a configuration key somehow
handler = self.WebSocketWSGIHandler(
self.rfile, self.wfile, self.get_stderr(), self.get_environ()
)
handler.request_handler = self # backpointer for logging
handler.run(self.server.get_app())
def run(self):
server = self._wsgi_server(
(self.hostname, self.port),
WSGIRequestHandler,
)
server.set_app(self.application)
self._print(f"Serving on {self.hostname}:{self.port} (press ctrl-c to stop)...")
try:
server.serve_forever()
except KeyboardInterrupt:
self._print("\nStopping...")
def run(self, handler): # pragma: no cover
from wsgiref.simple_server import make_server, WSGIRequestHandler
if self.quiet:
class QuietHandler(WSGIRequestHandler):
def log_request(*args, **kw): pass
self.options['handler_class'] = QuietHandler
srv = make_server(self.host, self.port, handler, **self.options)
srv.serve_forever()
def main():
class Handler(wsgiref.simple_server.WSGIRequestHandler):
def log_message(self, format, *args):
# Override the default log_message method to avoid logging
# remote addresses.
sys.stderr.write("[%s] %s\n" % (self.log_date_time_string(),
format % args))
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument(
'--host',
default='0.0.0.0',
help='host name to which the server should bind')
parser.add_argument(
'--port',
type=int,
default=8888,
help='port to which the server should bind')
webbrowser.open(auth_url, new=1, autoraise=True)
print(authorization_prompt_message.format(url=auth_url))
local_server.handle_request()
# Note: using https here because oauthlib is very picky that
# OAuth 2.0 should only occur over https.
authorization_response = wsgi_app.last_request_uri.replace(
'http', 'https')
self.fetch_token(authorization_response=authorization_response)
return self.credentials
class _WSGIRequestHandler(wsgiref.simple_server.WSGIRequestHandler):
"""Custom WSGIRequestHandler.
Uses a named logger instead of printing to stderr.
"""
def log_message(self, format, *args):
# pylint: disable=redefined-builtin
# (format is the argument name defined in the superclass.)
_LOGGER.info(format, *args)
class _RedirectWSGIApp(object):
"""WSGI app to handle the authorization redirect.
Stores the request URI and displays the given success message.
"""
allow_reuse_address = True
bottle_server = self
def handle_error(self, request, client_address):
pass
def serve_forever(self, poll_interval=0.5):
try:
wsgiref.simple_server.WSGIServer.serve_forever(self, poll_interval)
finally:
# Once shutdown is called, we need to close the session.
# If the session is not closed properly, it might raise warnings,
# or even lock the database.
self.bottle_server.close_session()
class Handler(wsgiref.simple_server.WSGIRequestHandler):
def address_string(self):
return self.client_address[0]
def log_request(*args, **kwargs): # pylint: disable=no-method-argument
if not self.quiet:
return wsgiref.simple_server.WSGIRequestHandler.log_request(*args,
**kwargs)
server = wsgiref.simple_server.make_server(
host=self.host,
port=self.port,
app=app,
server_class=Server,
handler_class=Handler)
self.proxy.server = server
self.proxy._started.put(True)
server.serve_forever(poll_interval=0.1)
def __init__(self, *args, **kwargs):
if kwargs.pop('ipv6', False):
self.address_family = socket.AF_INET6
super(WSGIServer, self).__init__(*args, **kwargs)
def server_bind(self):
"""Override server_bind to store the server name."""
try:
super(WSGIServer, self).server_bind()
except Exception as e:
raise WSGIServerException(e)
self.setup_environ()
class WSGIRequestHandler(simple_server.WSGIRequestHandler, object):
def __init__(self, *args, **kwargs):
from django.conf import settings
self.admin_static_prefix = urljoin(settings.STATIC_URL, 'admin/')
# We set self.path to avoid crashes in log_message() on unsupported
# requests (like "OPTIONS").
self.path = ''
self.style = color_style()
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
def address_string(self):
# Short-circuit parent method to not call socket.getfqdn
return self.client_address[0]
def log_message(self, format, *args):
# Don't bother logging requests for admin images or the favicon.