-
-
Save juanbenitopr/cbfef0f29668fc95852684c41b759b1c to your computer and use it in GitHub Desktop.
Example of how WSGI python server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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