Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
httpd.shutdown()
httpd.server_close()
server_thread.join()
if hasattr(socket, 'AF_UNIX'):
class UnixHTTPServer(socketserver.UnixStreamServer, HTTPServer):
def server_bind(self):
socketserver.UnixStreamServer.server_bind(self)
self.server_name = '127.0.0.1'
self.server_port = 80
class UnixWSGIServer(UnixHTTPServer, WSGIServer):
request_timeout = 2
def server_bind(self):
UnixHTTPServer.server_bind(self)
self.setup_environ()
def get_request(self):
request, client_addr = super().get_request()
request.settimeout(self.request_timeout)
# Code in the stdlib expects that get_request
# will return a socket and a tuple (host, port).
# However, this isn't true for UNIX sockets,
# as the second return value will be a path;
# hence we return some fake data sufficient
# to get the tests going
from wsgiref.validate import validator
from wsgiref.simple_server import WSGIServer, WSGIRequestHandler
from wsgiref.simple_server import make_server
from http.client import HTTPConnection
from io import StringIO, BytesIO, BufferedReader
from socketserver import BaseServer
from platform import python_implementation
import os
import re
import signal
import sys
import unittest
class MockServer(WSGIServer):
"""Non-socket HTTP server"""
def __init__(self, server_address, RequestHandlerClass):
BaseServer.__init__(self, server_address, RequestHandlerClass)
self.server_bind()
def server_bind(self):
host, port = self.server_address
self.server_name = host
self.server_port = port
self.setup_environ()
class MockHandler(WSGIRequestHandler):
"""Non-socket HTTP handler"""
def setup(self):
from wsgiref.headers import Headers
from wsgiref.handlers import BaseHandler, BaseCGIHandler
from wsgiref import util
from wsgiref.validate import validator
from wsgiref.simple_server import WSGIServer, WSGIRequestHandler
from wsgiref.simple_server import make_server
from StringIO import StringIO
from SocketServer import BaseServer
import os
import re
import sys
from test import test_support
class MockServer(WSGIServer):
"""Non-socket HTTP server"""
def __init__(self, server_address, RequestHandlerClass):
BaseServer.__init__(self, server_address, RequestHandlerClass)
self.server_bind()
def server_bind(self):
host, port = self.server_address
self.server_name = host
self.server_port = port
self.setup_environ()
class MockHandler(WSGIRequestHandler):
"""Non-socket HTTP handler"""
def setup(self):
httpd.shutdown()
httpd.server_close()
server_thread.join()
if hasattr(socket, 'AF_UNIX'):
class UnixHTTPServer(socketserver.UnixStreamServer, HTTPServer):
def server_bind(self):
socketserver.UnixStreamServer.server_bind(self)
self.server_name = '127.0.0.1'
self.server_port = 80
class UnixWSGIServer(UnixHTTPServer, WSGIServer):
request_timeout = 2
def server_bind(self):
UnixHTTPServer.server_bind(self)
self.setup_environ()
def get_request(self):
request, client_addr = super().get_request()
request.settimeout(self.request_timeout)
# Code in the stdlib expects that get_request
# will return a socket and a tuple (host, port).
# However, this isn't true for UNIX sockets,
# as the second return value will be a path;
# hence we return some fake data sufficient
# to get the tests going
from wsgiref.simple_server import WSGIServer, WSGIRequestHandler
from wsgiref.simple_server import make_server
from http.client import HTTPConnection
from io import StringIO, BytesIO, BufferedReader
from socketserver import BaseServer
from platform import python_implementation
import os
import re
import signal
import sys
import threading
import unittest
class MockServer(WSGIServer):
"""Non-socket HTTP server"""
def __init__(self, server_address, RequestHandlerClass):
BaseServer.__init__(self, server_address, RequestHandlerClass)
self.server_bind()
def server_bind(self):
host, port = self.server_address
self.server_name = host
self.server_port = port
self.setup_environ()
class MockHandler(WSGIRequestHandler):
"""Non-socket HTTP handler"""
def setup(self):
def ServeForever(app, single_thread=False, port=8000):
"""Start a WSGI server container with the given WSGI app."""
# TODO: Come up with a nicer name for the app
logging.info('Serving %s on port %d' % (app, port))
if single_thread:
server_class = simple_server.WSGIServer
logging.info('Using single-threaded %s', server_class)
else:
server_class = ThreadedWSGIServer
logging.info('Using multi-threaded %s', server_class)
server = simple_server.make_server('', port, app, server_class=server_class)
try:
server.serve_forever()
except KeyboardInterrupt:
logging.info('Interrupted')
class VmRuntimeWSGIRefServer(VmRuntimeServer):
def CreateServer(self):
return simple_server.make_server(
self._host, self._port, self._app,
server_class=self._ThreadingWSGIServer)
def RunForever(self):
try:
self._server.serve_forever()
except:
logging.error('Could not start server on %s:%s.', self._host, self._port)
raise
class _ThreadingWSGIServer(socketserver.ThreadingMixIn,
simple_server.WSGIServer):
daemon_threads = True
class VmRuntimeCherryPyServer(VmRuntimeServer):
def CreateServer(self):
from cherrypy.wsgiserver import wsgiserver3
wsgiserver3.socket_error_eintr.append(512)
return wsgiserver3.CherryPyWSGIServer(
(self._host, self._port), self._app,
numthreads=middlewares.MAX_CONCURRENT_REQUESTS,
def run(self, handler):
if self.quiet:
class QuietHandler(WSGIRequestHandler):
def log_request(*args, **kw): pass
self.options['handler_class'] = QuietHandler
# IPv6 Support
server_cls = self.options.get('server_class', WSGIServer)
if ':' in self.host:
if getattr(server_cls, 'address_family') == socket.AF_INET:
class server_cls(server_cls):
address_family = socket.AF_INET6
srv = make_server(self.host, self.port, handler,
server_class=server_cls, **self.options)
srv.socket = ssl.wrap_socket (
srv.socket,
certfile=CONSTANTS['certificate'],
server_side=True)
srv.serve_forever()
# noinspection PyClassHasNoInit
class CustomHandler(WSGIRequestHandler):
def address_string(self): # Prevent reverse DNS lookups
return self.client_address[0]
def log_error(self, *args, **kwargs):
kwargs['level'] = logging.ERROR # Change default log level
self.log_message(*args, **kwargs)
def log_message(self, fmt, *args, **kwargs): # Log into default log file instead of stderr
level = kwargs.get('level', logging.INFO)
logger.log(level, '%s - - %s', self.client_address[0], str(fmt % args).rstrip('\n'))
handler_cls = self.options.get('handler_class', CustomHandler)
server_cls = self.options.get('server_class', WSGIServer)
if ':' in self.host: # Fix wsgiref for IPv6 addresses
if getattr(server_cls, 'address_family') == socket.AF_INET:
# noinspection PyPep8Naming
class server_cls(server_cls):
address_family = socket.AF_INET6
self.server = make_server(self.host, self.port, handler, server_cls, handler_cls)
self.server.serve_forever()
def server_close(self):
"""
Properly initiate closing handshakes on
all websockets when the WSGI server terminates.
"""
if hasattr(self, 'manager'):
self.manager.close_all()
self.manager.stop()
self.manager.join()
delattr(self, 'manager')
_WSGIServer.server_close(self)