Skip to content

Instantly share code, notes, and snippets.

@juanbenitopr
Last active May 23, 2020 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanbenitopr/cbfef0f29668fc95852684c41b759b1c to your computer and use it in GitHub Desktop.
Save juanbenitopr/cbfef0f29668fc95852684c41b759b1c to your computer and use it in GitHub Desktop.
Example of how WSGI python server
# Sibling Fake of gunicorn
import os
import sys
from typing import List, Tuple
class Funicorn:
def __init__(self, app_path: str):
self.application = load_application(application_path)
self.buffer_output = sys.stdout.buffer
self.set_environ()
def set_environ(self):
self.environ = {**os.environ}
self.environ['wsgi.input'] = sys.stdin.buffer
self.environ['wsgi.errors'] = sys.stderr.buffer
def write(self, data: bytes):
self.buffer_output.write(data)
def write_headers(self, status, headers: List[Tuple(str, str)]):
self.write(f'Status: {status}\r\n'.encode())
for hkey, hvalue in headers:
self.write(f'{hkey}: {hvalue}\r\n'.encode())
self.write('\r\n'.encode())
def start_response(self, status, response_headers: List[Tuple(str, str)], **kwargs):
self.write_headers(status, response_headers)
def finish(self):
self.buffer_output.flush()
def run(self):
response_body = self.application({**self.environ}, self.start_response)
self.write(response_body)
if __name__ == 'main':
application_path = 'path_to_fango.app'
funicorn = Funicorn(application_path)
funicorn.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment