move SocketHTTPServer to isso/wsgi.py

This commit is contained in:
Martin Zimmermann 2013-11-17 11:57:41 +01:00
parent 96f29e1cc5
commit b9158a660c
2 changed files with 47 additions and 40 deletions

View File

@ -40,7 +40,7 @@ except ImportError:
import sys import sys
import os import os
import socket import errno
import logging import logging
import tempfile import tempfile
@ -54,7 +54,7 @@ from werkzeug.exceptions import HTTPException, InternalServerError
from werkzeug.wsgi import SharedDataMiddleware from werkzeug.wsgi import SharedDataMiddleware
from werkzeug.local import Local, LocalManager from werkzeug.local import Local, LocalManager
from werkzeug.serving import run_simple, WSGIRequestHandler from werkzeug.serving import run_simple
from werkzeug.contrib.fixers import ProxyFix from werkzeug.contrib.fixers import ProxyFix
local = Local() local = Local()
@ -211,43 +211,13 @@ def main():
run_simple(host, port, make_app(conf), threaded=True, run_simple(host, port, make_app(conf), threaded=True,
use_reloader=conf.getboolean('server', 'reload')) use_reloader=conf.getboolean('server', 'reload'))
else: else:
try:
from socketserver import ThreadingMixIn
from http.server import HTTPServer
except ImportError:
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer
class SocketWSGIRequestHandler(WSGIRequestHandler):
def run_wsgi(self):
self.client_address = ("<local>", 0)
super(SocketWSGIRequestHandler, self).run_wsgi()
class SocketHTTPServer(HTTPServer, ThreadingMixIn):
multithread = True
multiprocess = False
allow_reuse_address = 1
address_family = socket.AF_UNIX
request_queue_size = 128
def __init__(self, sock, app):
HTTPServer.__init__(self, sock, SocketWSGIRequestHandler)
self.app = app
self.ssl_context = None
self.shutdown_signal = False
sock = conf.get("server", "listen").partition("unix://")[2] sock = conf.get("server", "listen").partition("unix://")[2]
try: try:
os.unlink(sock) os.unlink(sock)
except OSError: except OSError as ex:
pass if ex.errno != errno.ENOENT:
raise
SocketHTTPServer(sock, make_app(conf)).serve_forever() wsgi.SocketHTTPServer(sock, make_app(conf)).serve_forever()
try: try:
import uwsgi import uwsgi

View File

@ -1,10 +1,19 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
import socket
try: try:
from urllib import quote
except ImportError:
from urllib.parse import quote from urllib.parse import quote
from socketserver import ThreadingMixIn
from http.server import HTTPServer
except ImportError:
from urllib import quote
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer
from werkzeug.serving import WSGIRequestHandler
from werkzeug.datastructures import Headers from werkzeug.datastructures import Headers
@ -23,10 +32,10 @@ def host(environ):
if environ['wsgi.url_scheme'] == 'https': if environ['wsgi.url_scheme'] == 'https':
if environ['SERVER_PORT'] != '443': if environ['SERVER_PORT'] != '443':
url += ':' + environ['SERVER_PORT'] url += ':' + environ['SERVER_PORT']
else: else:
if environ['SERVER_PORT'] != '80': if environ['SERVER_PORT'] != '80':
url += ':' + environ['SERVER_PORT'] url += ':' + environ['SERVER_PORT']
return url + quote(environ.get('SCRIPT_NAME', '')) return url + quote(environ.get('SCRIPT_NAME', ''))
@ -49,6 +58,7 @@ class SubURI(object):
class CORSMiddleware(object): class CORSMiddleware(object):
"""Add Cross-origin resource sharing headers to every request."""
def __init__(self, app, origin): def __init__(self, app, origin):
self.app = app self.app = app
@ -70,3 +80,30 @@ class CORSMiddleware(object):
return ['200 Ok'] return ['200 Ok']
return self.app(environ, add_cors_headers) return self.app(environ, add_cors_headers)
class SocketWSGIRequestHandler(WSGIRequestHandler):
def run_wsgi(self):
self.client_address = ("<local>", 0)
super(SocketWSGIRequestHandler, self).run_wsgi()
class SocketHTTPServer(HTTPServer, ThreadingMixIn):
"""
A simple SocketServer to serve werkzeug's WSGIRequesthandler.
"""
multithread = True
multiprocess = False
allow_reuse_address = 1
address_family = socket.AF_UNIX
request_queue_size = 128
def __init__(self, sock, app):
HTTPServer.__init__(self, sock, SocketWSGIRequestHandler)
self.app = app
self.ssl_context = None
self.shutdown_signal = False