2014-01-06 54 views

回答

0

不要嘗試,即使您嘗試過並可能以某種方式使其工作,它可能會是一個巨大的黑客攻擊。

嘗試查找已在WSGI服務器上工作的應用程序的版本。

如果你需要一個非常小的WSGI服務器,它只依賴於標準Python發行版的東西,然後使用wsgiref模塊中的WSGI服務器。

如果你需要一個簡單易用的WSGI框架,並嘗試和端口的應用程序,使用瓶。

0

如果BaseHTTPRequestHandler是很簡單的,你可以試着彎曲它WSGI應用程序,請參閱SVNOnline.wsgi

""" 
wsgi.py 
WSGI warp 
""" 

import os 
import wsgiref.util 

try: 
    from cStringIO import StringIO 
except ImportError: 
    from StringIO import StringIO 

class HeadsWarp(object): 
    environ = None 
    def __init__(self, environ): 
     self.environ = environ 
    def getheader(self, name): 
     name = name.upper().replace('-', '_') 
     return self.environ.get('HTTP_%s' % (name), self.environ.get(name)) 

class WSGIHandler(YourHTTPRequestHandler): 
    path = None 
    headers = None 

    response_code = 200 
    response_message = 'OK' 
    request_version = 'HTTP/1.0' 
    response_headermap = { 
          'Content-Type':'text/html' 
          } 

    def __init__(self, request, client_address, server): 
     self.request = request 
     self.client_address = client_address 
     self.server = server 
     self.setup() 
     try: 
      self.handle() 
     finally: 
      self.finish() 

    def setup(self): 
     pass 

    def finish(self): 
     pass 

    def handle(self): 
     pass 

    def handle_one_request(self): 
     pass 

    def send_response(self, code, message=None): 
     self.response_code = code 
     self.response_message = message 

    def send_header(self, keyword, value): 
     self.response_headermap[keyword] = value 

    @property 
    def response_headers(self): 
     return self.response_headermap.items() 

def application(environ, start_response):  
    handler = WSGIHandler(None, (environ['REMOTE_ADDR'], None), None) 
#  for k, v in environ.items(): 
#   print k, v 
    handler.path = environ['PATH_INFO'] + ('?' + environ['QUERY_STRING'] if environ['QUERY_STRING'] else '') 
    handler.rfile = environ['wsgi.input'] 
    handler.headers = HeadsWarp(environ) 
    handler.wfile = StringIO() 
    handler.request_version = environ['SERVER_PROTOCOL'] 
    handler.command = environ['REQUEST_METHOD'] 
    method = 'do_%s' % (environ['REQUEST_METHOD']) 
    if hasattr(handler, method): 
     handler_method = getattr(handler, method) 
     handler_method() 
    handler.wfile.seek(0) 
    res = handler.wfile.read()[2:] 
    if res.startswith('\n\n'): 
     res = res[2:] 
    if res: 
     handler.send_header("Content-Length", str(len(res))) 

    status = '%s %s' % (handler.response_code or 200, handler.response_message or 'OK') 
    headers = [(k, v) for k, v in handler.response_headers if not wsgiref.util.is_hop_by_hop(k)] 

    start_response(status, headers) 



    return [res, ] 

然後使用gunicorn -b 0.0.0.0:8000 wsgi:application進行測試。