Skip to content

Commit 05e0757

Browse files
committed
[python] Add FastPySGI server (WSGI)
1 parent f8aa3fc commit 05e0757

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM python:3.12-slim
2+
WORKDIR /app
3+
RUN pip install --no-cache-dir fastpysgi
4+
COPY src/Servers/FastPySGI-Server/app.py .
5+
ENTRYPOINT ["python3", "app.py", "8080"]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import fastpysgi
2+
3+
def app(environ, start_response):
4+
path = environ["PATH_INFO"]
5+
req_method = environ.get('REQUEST_METHOD', '')
6+
7+
if path == '/cookie':
8+
cookie_str = environ.get('HTTP_COOKIE', '')
9+
lines = [ ]
10+
for pair in cookie_str.split(';'):
11+
pair = pair.strip()
12+
eq = pair.find('=')
13+
if eq > 0:
14+
lines.append(f"{pair[:eq]}={pair[eq+1:]}")
15+
body = '\n'.join(lines) + '\n' if lines else ''
16+
start_response('200 OK', [ ('Content-Type', 'text/plain') ])
17+
return [ body.encode() ]
18+
19+
if path == '/echo':
20+
lines = [ ]
21+
for key, value in environ.items():
22+
header_name = None
23+
if key.startswith('HTTP_'):
24+
header_name = key[5:]
25+
if key == 'CONTENT_TYPE':
26+
header_name = 'Content-Type'
27+
if key == 'CONTENT_LENGTH':
28+
header_name = 'Content-Length'
29+
if header_name:
30+
header_name = header_name.replace('_', '-').title()
31+
lines.append(f"{header_name}: {value}")
32+
body = '\n'.join(lines) + '\n'
33+
start_response('200 OK', [ ('Content-Type', 'text/plain') ])
34+
return [ body.encode() ]
35+
36+
if req_method == 'POST':
37+
try:
38+
length = int(environ.get('CONTENT_LENGTH', 0) or 0)
39+
except ValueError:
40+
length = 0
41+
body = environ['wsgi.input'].read(length) if length > 0 else b''
42+
start_response('200 OK', [ ('Content-Type', 'text/plain') ])
43+
return [ body ]
44+
45+
start_response('200 OK', [ ('Content-Type', 'text/plain') ])
46+
return [ b'OK' ]
47+
48+
if __name__ == "__main__":
49+
import sys
50+
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8080
51+
fastpysgi.run(app, host="0.0.0.0", port=port, loglevel=0)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "FastPySGI",
3+
"language": "Python"
4+
}

0 commit comments

Comments
 (0)