admin管理员组文章数量:1124160
We need to write a Python server which maintains persistent connections so when another request comes, it will use the old connection instead of creating new connection. We used flask
and gunicorn
for this.
Python code:
from flask import Flask, request, make_response, jsonify
app = Flask(__name__)
@app.route('/v1')
def get_availability():
response = make_response("Custom Response", 204)
return response
@app.route('/v2')
def get_ping():
response = make_response("Custom Response", 200)
return response
@app.errorhandler(404)
def not_found(error):
return jsonify({'error': 'Custom message for unavailable path'}), 404
And we are running the python server using gunicorn as below
gunicorn --keyfile key.pem --certfile cert.pem --bind 127.0.0.1:8080 app:app
I wrote a simple shell script which sends 2 curl
commands as below.
curl -H "Connection: keep-alive" -H "Keep-Alive: timeout=5, max=100" https://127.0.0.1:8080/v1 -v -k
curl -H "Connection: keep-alive" -H "Keep-Alive: timeout=5, max=100" https://127.0.0.1:8080/v2 -v -k
When I run the shell script, I am getting the responses but TLS connections are getting closed after each curl command and new curl command is not reusing the connection as below
* Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / X25519 / RSASSA-PSS
* ALPN: server did not agree on a protocol. Uses default.
* Server certificate:
* subject: C=XX; L=Default City; O=Default Company Ltd
* start date: Jan 8 22:43:00 2025 GMT
* expire date: Jan 8 22:43:00 2026 GMT
* issuer: C=XX; L=Default City; O=Default Company Ltd
* SSL certificate verify result: self-signed certificate (18), continuing anyway.
* Certificate level 0: Public key type RSA (4096/152 Bits/secBits), signed using sha256WithRSAEncryption
* using HTTP/1.x
> GET /v1 HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/8.5.0
> Accept: */*
> Connection: keep-alive
> Keep-Alive: timeout=5, max=100
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
< HTTP/1.1 200 OK
< Server: gunicorn
< Date: Thu, 09 Jan 2025 18:09:21 GMT
< Connection: close
< Content-Type: text/html; charset=utf-8
< Content-Length: 15
<
* Closing connection
* TLSv1.3 (OUT), TLS alert, close notify (256):
Custom Response* Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / X25519 / RSASSA-PSS
* ALPN: server did not agree on a protocol. Uses default.
* Server certificate:
* subject: C=XX; L=Default City; O=Default Company Ltd
* start date: Jan 8 22:43:00 2025 GMT
* expire date: Jan 8 22:43:00 2026 GMT
* issuer: C=XX; L=Default City; O=Default Company Ltd
* SSL certificate verify result: self-signed certificate (18), continuing anyway.
* Certificate level 0: Public key type RSA (4096/152 Bits/secBits), signed using sha256WithRSAEncryption
* using HTTP/1.x
> GET /v1 HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/8.5.0
> Accept: */*
> Connection: keep-alive
> Keep-Alive: timeout=5, max=100
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
< HTTP/1.1 200 OK
< Server: gunicorn
< Date: Thu, 09 Jan 2025 18:09:21 GMT
< Connection: close
< Content-Type: text/html; charset=utf-8
< Content-Length: 15
<
* Closing connection
* TLSv1.3 (OUT), TLS alert, close notify (256):
I thought gunicorn
maintains persistent connections but it is closing the connections. Can anyone please let me know if there is anyway to maintain persistent TLS connections.
We need to write a Python server which maintains persistent connections so when another request comes, it will use the old connection instead of creating new connection. We used flask
and gunicorn
for this.
Python code:
from flask import Flask, request, make_response, jsonify
app = Flask(__name__)
@app.route('/v1')
def get_availability():
response = make_response("Custom Response", 204)
return response
@app.route('/v2')
def get_ping():
response = make_response("Custom Response", 200)
return response
@app.errorhandler(404)
def not_found(error):
return jsonify({'error': 'Custom message for unavailable path'}), 404
And we are running the python server using gunicorn as below
gunicorn --keyfile key.pem --certfile cert.pem --bind 127.0.0.1:8080 app:app
I wrote a simple shell script which sends 2 curl
commands as below.
curl -H "Connection: keep-alive" -H "Keep-Alive: timeout=5, max=100" https://127.0.0.1:8080/v1 -v -k
curl -H "Connection: keep-alive" -H "Keep-Alive: timeout=5, max=100" https://127.0.0.1:8080/v2 -v -k
When I run the shell script, I am getting the responses but TLS connections are getting closed after each curl command and new curl command is not reusing the connection as below
* Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / X25519 / RSASSA-PSS
* ALPN: server did not agree on a protocol. Uses default.
* Server certificate:
* subject: C=XX; L=Default City; O=Default Company Ltd
* start date: Jan 8 22:43:00 2025 GMT
* expire date: Jan 8 22:43:00 2026 GMT
* issuer: C=XX; L=Default City; O=Default Company Ltd
* SSL certificate verify result: self-signed certificate (18), continuing anyway.
* Certificate level 0: Public key type RSA (4096/152 Bits/secBits), signed using sha256WithRSAEncryption
* using HTTP/1.x
> GET /v1 HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/8.5.0
> Accept: */*
> Connection: keep-alive
> Keep-Alive: timeout=5, max=100
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
< HTTP/1.1 200 OK
< Server: gunicorn
< Date: Thu, 09 Jan 2025 18:09:21 GMT
< Connection: close
< Content-Type: text/html; charset=utf-8
< Content-Length: 15
<
* Closing connection
* TLSv1.3 (OUT), TLS alert, close notify (256):
Custom Response* Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / X25519 / RSASSA-PSS
* ALPN: server did not agree on a protocol. Uses default.
* Server certificate:
* subject: C=XX; L=Default City; O=Default Company Ltd
* start date: Jan 8 22:43:00 2025 GMT
* expire date: Jan 8 22:43:00 2026 GMT
* issuer: C=XX; L=Default City; O=Default Company Ltd
* SSL certificate verify result: self-signed certificate (18), continuing anyway.
* Certificate level 0: Public key type RSA (4096/152 Bits/secBits), signed using sha256WithRSAEncryption
* using HTTP/1.x
> GET /v1 HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/8.5.0
> Accept: */*
> Connection: keep-alive
> Keep-Alive: timeout=5, max=100
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
< HTTP/1.1 200 OK
< Server: gunicorn
< Date: Thu, 09 Jan 2025 18:09:21 GMT
< Connection: close
< Content-Type: text/html; charset=utf-8
< Content-Length: 15
<
* Closing connection
* TLSv1.3 (OUT), TLS alert, close notify (256):
I thought gunicorn
maintains persistent connections but it is closing the connections. Can anyone please let me know if there is anyway to maintain persistent TLS connections.
1 Answer
Reset to default 0I believe it's curl
that closes the connection, because the first one terminates and you launch a new one, which doesn't "remember" the connection left open by the first call. Try something like
curl -H "Connection: keep-alive" -H "Keep-Alive: timeout=5, max=100" -v -k \
https://127.0.0.1:8080/v1 https://127.0.0.1:8080/v1
to make both HTTP requests in the same curl
process. You should then see a line similar to the following:
* Re-using existing connection with host 127.0.0.1:8080
本文标签: pythongunicorn is not maintaining persistent TLS connectionsStack Overflow
版权声明:本文标题:python - gunicorn is not maintaining persistent TLS connections - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736617664a1945506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论