Skip to content

Commit 2cca31d

Browse files
committed
move all server-related files into /wsgiasgi/server package
add top-level setup.py to install the wsgiasgi server package use top-level run.py to start up a test server with a test app
1 parent 14f2710 commit 2cca31d

File tree

11 files changed

+40
-23
lines changed

11 files changed

+40
-23
lines changed

app.py renamed to run.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask import Flask, request
2+
from wsgi.server import WSGIServer
23

34

45
app = Flask(__name__)
@@ -14,3 +15,8 @@ def root():
1415
def create():
1516
print(f"Called create endpoint with data {request.data}.")
1617
return "hello from /create"
18+
19+
20+
if __name__ == "__main__":
21+
server = WSGIServer("127.0.0.1", 5000, app)
22+
server.serve_forever()

setup.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from setuptools import setup, find_packages
2+
3+
4+
setup(
5+
name="wsgi",
6+
description="A tutorial implementation of a WSGI server and application.",
7+
version="0.0.1",
8+
packages=find_packages(),
9+
)

wsgi/__init__.py

Whitespace-only changes.

wsgi/server/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .server import WSGIServer

http_parse.py renamed to wsgi/server/http_parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from splitbuffer import SplitBuffer
1+
from .splitbuffer import SplitBuffer
22

33

44
class HttpRequestParser:
File renamed without changes.

server.py renamed to wsgi/server/server.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
import socket
33
import threading
44
from io import BytesIO
5-
from http_parse import HttpRequestParser
6-
from wsgi import WSGIRequest, WSGIResponse
7-
from app import app
5+
from .http_parse import HttpRequestParser
6+
from .wsgi import WSGIRequest, WSGIResponse
87

98

109
class Session:
11-
def __init__(self, client_socket, address):
10+
def __init__(self, client_socket, address, app):
1211
self.client_socket = client_socket
1312
self.address = address
13+
self.app = app
1414
self.parser = HttpRequestParser(self)
1515
self.request = WSGIRequest()
1616
self.response = WSGIResponse()
@@ -45,24 +45,26 @@ def on_body(self, body: bytes):
4545
def on_message_complete(self):
4646
print("Received request completely.")
4747
environ = self.request.to_environ()
48-
body_chunks = app(environ, self.response.start_response)
48+
body_chunks = self.app(environ, self.response.start_response)
4949
print("App callable has returned.")
5050
self.response.body = b"".join(body_chunks)
5151
self.client_socket.send(self.response.to_http())
5252

5353

54-
def serve_forever(host: str, port: int):
55-
server_socket = socket.socket()
56-
server_socket.bind((host, port))
57-
server_socket.listen(1)
54+
class WSGIServer:
55+
def __init__(self, host: str, port: int, app):
56+
self.host = host
57+
self.port = port
58+
self.app = app
5859

59-
while True:
60-
client_socket, address = server_socket.accept()
61-
print(f"Socket established with {address}.")
62-
session = Session(client_socket, address)
63-
t = threading.Thread(target=session.run)
64-
t.start()
60+
def serve_forever(self):
61+
server_socket = socket.socket()
62+
server_socket.bind((self.host, self.port))
63+
server_socket.listen(1)
6564

66-
67-
if __name__ == "__main__":
68-
serve_forever("127.0.0.1", 5000)
65+
while True:
66+
client_socket, address = server_socket.accept()
67+
print(f"Socket established with {address}.")
68+
session = Session(client_socket, address, self.app)
69+
t = threading.Thread(target=session.run)
70+
t.start()
File renamed without changes.

test_http_parse.py renamed to wsgi/server/test_http_parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from http_parse import HttpRequestParser
2+
from .http_parse import HttpRequestParser
33

44

55
class TestParserProtocol:

test_splitbuffer.py renamed to wsgi/server/test_splitbuffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from splitbuffer import SplitBuffer
1+
from .splitbuffer import SplitBuffer
22

33
def test_splitbuffer():
44
buffer = SplitBuffer()

0 commit comments

Comments
 (0)