Skip to content

Commit 7dc7278

Browse files
authored
Merge pull request #605 from remittor-pr/fastpysgi-0.6
[python] FastPySGI: update version to v0.6 and enable JSON-TLS
2 parents 9a26301 + b770796 commit 7dc7278

8 files changed

Lines changed: 49 additions & 88 deletions

File tree

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
FROM python:3.13-slim
1+
FROM python:3.11-slim
2+
RUN apt-get update \
3+
&& apt-get install -y --no-install-recommends git build-essential \
4+
&& rm -rf /var/lib/apt/lists/*
5+
ENV PATH="/usr/local/bin:$PATH"
26
WORKDIR /app
37
COPY requirements.txt .
48
RUN pip install --no-cache-dir -r requirements.txt
59
COPY . .
6-
EXPOSE 8080
7-
CMD [ "python", "./app.py" ]
10+
EXPOSE 8080 8081
11+
CMD [ "python", "app.py" ]

frameworks/fastpysgi-asgi/app.py

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def get_header(scope: dict, name: str, def_value: str):
127127

128128
def check_accept_encoding(scope, substr):
129129
aenc = get_header(scope, 'Accept-Encoding', '')
130-
if aenc and substr == "*":
130+
if aenc and substr == "":
131131
return True
132132
if aenc and substr in aenc:
133133
return True
@@ -159,35 +159,11 @@ async def baseline11(scope, receive, send):
159159
req_method = scope.get('method', '')
160160
query_params = parse_qs(scope.get('query_string', b'').decode())
161161
total = 0
162-
for v in query_params.values():
163-
try:
164-
total += int(v[0])
165-
except ValueError:
166-
pass
162+
for val in query_params.values():
163+
total += int(val[0])
167164
if req_method == "POST":
168-
body = b''
169-
while True:
170-
message = await receive()
171-
body += message.get('body', b'')
172-
if not message.get('more_body', False):
173-
break
174-
if body:
175-
try:
176-
total += int(body.decode().strip())
177-
except UnicodeDecodeError:
178-
pass
179-
except ValueError:
180-
pass
181-
return text_resp(str(total))
182-
183-
async def baseline2(scope, receive, send):
184-
query_params = parse_qs(scope.get('query_string', b'').decode())
185-
total = 0
186-
for v in query_params.values():
187-
try:
188-
total += int(v[0])
189-
except ValueError:
190-
pass
165+
message = await receive()
166+
total += int(message.get('body', b''))
191167
return text_resp(str(total))
192168

193169
async def json_endpoint(scope, receive, send):
@@ -270,7 +246,6 @@ async def upload_endpoint(scope, receive, send):
270246
ROUTES = {
271247
'/pipeline': pipeline,
272248
'/baseline11': baseline11,
273-
'/baseline2': baseline2,
274249
'/json/': json_endpoint,
275250
'/json-comp/': json_endpoint,
276251
'/upload': upload_endpoint,
@@ -320,10 +295,15 @@ async def app(scope, receive, send):
320295
if __name__ == "__main__":
321296
import fastpysgi
322297

323-
host = '0.0.0.0'
324-
port = 8080
298+
certfile = os.environ.get("TLS_CERT", "/certs/server.crt")
299+
keyfile = os.environ.get("TLS_KEY" , "/certs/server.key")
300+
301+
fastpysgi.server.delete_all_binds()
302+
fastpysgi.server.add_bind('0.0.0.0', 8080)
303+
fastpysgi.server.add_bind('0.0.0.0', 8081, (certfile, keyfile, None))
325304

326305
fastpysgi.server.read_buffer_size = 256*1024
306+
fastpysgi.server.max_content_length = 31_000_000
327307
fastpysgi.server.backlog = 16*1024
328308
fastpysgi.server.loop_timeout = 1
329-
fastpysgi.run(app, host, port, workers = WRK_COUNT, loglevel = 0)
309+
fastpysgi.run(app, workers = WRK_COUNT, loglevel = 0)

frameworks/fastpysgi-asgi/meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"enabled": true,
99
"tests": [
1010
"baseline",
11-
"pipelined",
1211
"limited-conn",
1312
"json",
1413
"json-comp",
14+
"json-tls",
1515
"upload",
1616
"api-4",
1717
"api-16",
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
fastpysgi==0.5
2-
orjson==3.10.15
3-
asyncpg==0.30.0
1+
#fastpysgi==0.6 # v0.6 is bugged
2+
git+https://github.com/remittor/fastpysgi.git@21095fe037fabb0d324814ccacfff3f4b164128d#egg=fastpysgi
3+
orjson==3.11.8
4+
asyncpg==0.31.0
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
FROM python:3.13-slim
1+
FROM python:3.13-slim-bookworm
2+
RUN apt-get update \
3+
&& apt-get install -y --no-install-recommends git build-essential \
4+
&& apt-get clean \
5+
&& rm -rf /var/lib/apt/lists/*
26
WORKDIR /app
37
COPY requirements.txt .
48
RUN pip install --no-cache-dir -r requirements.txt
59
COPY . .
6-
EXPOSE 8080
7-
CMD [ "python", "./app.py" ]
10+
EXPOSE 8080 8081
11+
CMD [ "python", "app.py" ]

frameworks/fastpysgi-wsgi/app.py

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def get_path_tail(env):
112112

113113
def check_accept_encoding(env, substr):
114114
aenc = env.get('HTTP_ACCEPT_ENCODING', '')
115-
if aenc and substr == "*":
115+
if aenc and substr == "":
116116
return True
117117
if aenc and substr in aenc:
118118
return True
@@ -144,31 +144,10 @@ def baseline11(env):
144144
req_method = env.get('REQUEST_METHOD', '')
145145
query_params = parse_qs(env.get('QUERY_STRING', ''))
146146
total = 0
147-
for v in query_params.values():
148-
try:
149-
total += int(v[0])
150-
except ValueError:
151-
pass
147+
for val in query_params.values():
148+
total += int(val[0])
152149
if req_method == "POST":
153-
wsgi_input = env['wsgi.input']
154-
body = wsgi_input.read(16000)
155-
if body:
156-
try:
157-
total += int(body.decode().strip())
158-
except UnicodeDecodeError:
159-
pass
160-
except ValueError:
161-
pass
162-
return text_resp(str(total))
163-
164-
def baseline2(env):
165-
query_params = parse_qs(env.get('QUERY_STRING', ''))
166-
total = 0
167-
for v in query_params.values():
168-
try:
169-
total += int(v[0])
170-
except ValueError:
171-
pass
150+
total += int(env['wsgi.input'].read(100))
172151
return text_resp(str(total))
173152

174153
def json_endpoint(env):
@@ -238,28 +217,14 @@ def static_file_endpoint(env):
238217
return make_resp(200, [ ('Content-Type', entry['type']) ], entry['data'], contenc = entry['enc'])
239218

240219

241-
READ_BUF_SIZE = 256*1024
242-
243220
def upload_endpoint(env):
244-
wsgi_input = env["wsgi.input"]
245-
content_length = int(env.get("CONTENT_LENGTH", -1))
246-
size = 0
247-
if content_length != 0:
248-
while True:
249-
to_read = min(READ_BUF_SIZE, content_length - size) if content_length > 0 else READ_BUF_SIZE
250-
chunk = wsgi_input.read(to_read)
251-
if not chunk:
252-
break
253-
size += len(chunk)
254-
if content_length > 0 and size >= content_length:
255-
break
221+
size = env["wsgi.input"].getbuffer().nbytes
256222
return text_resp(str(size))
257223

258224

259225
ROUTES = {
260226
'/pipeline': pipeline,
261227
'/baseline11': baseline11,
262-
'/baseline2': baseline2,
263228
'/json/': json_endpoint,
264229
'/json-comp/': json_endpoint,
265230
'/upload': upload_endpoint,
@@ -301,9 +266,14 @@ def app(env, start_response):
301266
if __name__ == "__main__":
302267
import fastpysgi
303268

304-
host = '0.0.0.0'
305-
port = 8080
269+
certfile = os.environ.get("TLS_CERT", "/certs/server.crt")
270+
keyfile = os.environ.get("TLS_KEY" , "/certs/server.key")
271+
272+
fastpysgi.server.delete_all_binds()
273+
fastpysgi.server.add_bind('0.0.0.0', 8080)
274+
fastpysgi.server.add_bind('0.0.0.0', 8081, (certfile, keyfile, None))
306275

307-
fastpysgi.server.read_buffer_size = READ_BUF_SIZE
276+
fastpysgi.server.read_buffer_size = 256*1024
277+
fastpysgi.server.max_content_length = 31_000_000
308278
fastpysgi.server.backlog = 16*1024
309-
fastpysgi.run(app, host, port, workers = WRK_COUNT, loglevel = 0)
279+
fastpysgi.run(app, workers = WRK_COUNT, loglevel = 0)

frameworks/fastpysgi-wsgi/meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"limited-conn",
1313
"json",
1414
"json-comp",
15+
"json-tls",
1516
"upload",
1617
"api-4",
1718
"api-16",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
fastpysgi==0.5
1+
#fastpysgi==0.6 # v0.6 is bugged
2+
git+https://github.com/remittor/fastpysgi.git@fd71875920d55c03b347ff5b19dc9d93b5983a83#egg=fastpysgi
23
orjson==3.10.15
34
psycopg[binary]==3.2.4
45
psycopg_pool==3.2.6

0 commit comments

Comments
 (0)