Last active
June 20, 2021 13:59
-
-
Save logston/8dfc7d59e0909c787bfe to your computer and use it in GitHub Desktop.
My Own WSGI Server + WSGI App In Less Than 35 Lines
This file contains hidden or 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
| import socket | |
| def app(environ, start_response): | |
| status_n_headers = ['HTTP/1.1 200 OK', 'Content-Type: text/plain\r\n',] | |
| body = ['bytes', 'and', 'more','bytes'] | |
| return status_n_headers + body | |
| def start_response(): | |
| # waiting for web3 | |
| pass | |
| def server(app): | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| s.bind(('', 8888)) | |
| s.listen(1) | |
| while True: | |
| conn, addr = s.accept() | |
| print('Connection from: {}'.format(addr)) | |
| data = bytes() | |
| while True: | |
| new_data = conn.recv(1024) | |
| data += new_data | |
| if len(new_data) < 1024: | |
| break | |
| environ = {} | |
| result = app(environ, start_response) | |
| return_data = bytes('\r\n'.join(line for line in result), 'utf-8') | |
| conn.sendall(return_data) | |
| conn.close() | |
| server(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment