Skip to content

Instantly share code, notes, and snippets.

@juanbenitopr
Last active May 23, 2020
Embed
What would you like to do?
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