admin管理员组文章数量:1126140
We need to write a flask application which should not close the connection after serving the client. Below is the python code
from flask import Flask, request, make_response, jsonify
from werkzeug.serving import WSGIRequestHandler
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
if __name__ == "__main__":
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run(ssl_context=('cert.pem', 'key.pem'), port=8080)
I used below curl command to send a request.
curl --cert cert.pem --key key.pem https://127.0.0.1:8080/v1 -v -k
It is working and I am getting the response but the connection is not keep alive.
Below is curl command outout
* 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: */*
>
* 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 204 NO CONTENT
< Server: Werkzeug/3.1.3 Python/3.11.9
< Date: Thu, 09 Jan 2025 01:12:02 GMT
< Content-Type: text/html; charset=utf-8
< Connection: close
<
* Closing connection
* TLSv1.3 (OUT), TLS alert, close notify (256):
Through tcpdump, I verified and it is infact flask application is sending reset connection.
ip-10-10-10-10:100$ sudo tcpdump -i lo port 8080
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes
01:00:01.142531 IP localhost.47782 > localhost.webcache: Flags [S], seq 3077322951, win 65495, options [mss 65495,sackOK,TS val 3930690793 ecr 0,nop,wscale 7], length 0
01:00:01.142547 IP localhost.webcache > localhost.47782: Flags [S.], seq 2243081118, ack 3077322952, win 65483, options [mss 65495,sackOK,TS val 3930690793 ecr 3930690793,nop,wscale 7], length 0
01:00:01.142561 IP localhost.47782 > localhost.webcache: Flags [.], ack 1, win 512, options [nop,nop,TS val 3930690793 ecr 3930690793], length 0
01:00:01.145999 IP localhost.47782 > localhost.webcache: Flags [P.], seq 1:518, ack 1, win 512, options [nop,nop,TS val 3930690796 ecr 3930690793], length 517: HTTP
01:00:01.146012 IP localhost.webcache > localhost.47782: Flags [.], ack 518, win 508, options [nop,nop,TS val 3930690796 ecr 3930690796], length 0
01:00:01.156443 IP localhost.webcache > localhost.47782: Flags [P.], seq 1:2198, ack 518, win 512, options [nop,nop,TS val 3930690807 ecr 3930690796], length 2197: HTTP
01:00:01.156465 IP localhost.47782 > localhost.webcache: Flags [.], ack 2198, win 499, options [nop,nop,TS val 3930690807 ecr 3930690807], length 0
01:00:01.157599 IP localhost.47782 > localhost.webcache: Flags [P.], seq 518:598, ack 2198, win 512, options [nop,nop,TS val 3930690808 ecr 3930690807], length 80: HTTP
01:00:01.157743 IP localhost.webcache > localhost.47782: Flags [P.], seq 2198:2453, ack 598, win 512, options [nop,nop,TS val 3930690808 ecr 3930690808], length 255: HTTP
01:00:01.157786 IP localhost.47782 > localhost.webcache: Flags [P.], seq 598:722, ack 2453, win 511, options [nop,nop,TS val 3930690808 ecr 3930690808], length 124: HTTP
01:00:01.157798 IP localhost.webcache > localhost.47782: Flags [P.], seq 2453:2708, ack 722, win 512, options [nop,nop,TS val 3930690808 ecr 3930690808], length 255: HTTP
01:00:01.169686 IP localhost.webcache > localhost.47782: Flags [FP.], seq 2708:2940, ack 722, win 512, options [nop,nop,TS val 3930690820 ecr 3930690808], length 232: HTTP
01:00:01.169801 IP localhost.47782 > localhost.webcache: Flags [.], ack 2941, win 512, options [nop,nop,TS val 3930690820 ecr 3930690808], length 0
01:00:01.169867 IP localhost.47782 > localhost.webcache: Flags [P.], seq 722:746, ack 2941, win 512, options [nop,nop,TS val 3930690820 ecr 3930690808], length 24: HTTP
01:00:01.169878 IP localhost.webcache > localhost.47782: Flags [R], seq 2243084059, win 0, length 0
01:00:41.470495 IP localhost.59314 > localhost.webcache: Flags [S], seq 2761050291, win 65495, options [mss 65495,sackOK,TS val 3930731121 ecr 0,nop,wscale 7], length 0
01:00:41.470509 IP localhost.webcache > localhost.59314: Flags [S.], seq 3096878784, ack 2761050292, win 65483, options [mss 65495,sackOK,TS val 3930731121 ecr 3930731121,nop,wscale 7], length 0
01:00:41.470521 IP localhost.59314 > localhost.webcache: Flags [.], ack 1, win 512, options [nop,nop,TS val 3930731121 ecr 3930731121], length 0
01:00:41.473914 IP localhost.59314 > localhost.webcache: Flags [P.], seq 1:518, ack 1, win 512, options [nop,nop,TS val 3930731124 ecr 3930731121], length 517: HTTP
01:00:41.473927 IP localhost.webcache > localhost.59314: Flags [.], ack 518, win 508, options [nop,nop,TS val 3930731124 ecr 3930731124], length 0
01:00:41.482397 IP localhost.webcache > localhost.59314: Flags [P.], seq 1:2198, ack 518, win 512, options [nop,nop,TS val 3930731133 ecr 3930731124], length 2197: HTTP
01:00:41.482413 IP localhost.59314 > localhost.webcache: Flags [.], ack 2198, win 499, options [nop,nop,TS val 3930731133 ecr 3930731133], length 0
01:00:41.483329 IP localhost.59314 > localhost.webcache: Flags [P.], seq 518:598, ack 2198, win 512, options [nop,nop,TS val 3930731134 ecr 3930731133], length 80: HTTP
01:00:41.483483 IP localhost.webcache > localhost.59314: Flags [P.], seq 2198:2453, ack 598, win 512, options [nop,nop,TS val 3930731134 ecr 3930731134], length 255: HTTP
01:00:41.483509 IP localhost.59314 > localhost.webcache: Flags [P.], seq 598:735, ack 2453, win 511, options [nop,nop,TS val 3930731134 ecr 3930731134], length 137: HTTP
01:00:41.483539 IP localhost.webcache > localhost.59314: Flags [P.], seq 2453:2708, ack 735, win 511, options [nop,nop,TS val 3930731134 ecr 3930731134], length 255: HTTP
01:00:41.495010 IP localhost.webcache > localhost.59314: Flags [FP.], seq 2708:2891, ack 735, win 512, options [nop,nop,TS val 3930731145 ecr 3930731134], length 183: HTTP
01:00:41.495079 IP localhost.59314 > localhost.webcache: Flags [.], ack 2892, win 512, options [nop,nop,TS val 3930731145 ecr 3930731134], length 0
01:00:41.495176 IP localhost.59314 > localhost.webcache: Flags [P.], seq 735:759, ack 2892, win 512, options [nop,nop,TS val 3930731145 ecr 3930731134], length 24: HTTP
01:00:41.495185 IP localhost.webcache > localhost.59314: Flags [R], seq 3096881676, win 0, length 0
Flags in last line of tcpdump is [R] which is reset connection. Can anyone please help me to understand why flask is closing the connection even though after setting WSGIRequestHandler.protocol_version to HTTP/1.1 in the code and is there anyway to fix this.
本文标签:
版权声明:本文标题:python - Flask is closing https connections even after setting WSGIRequestHandler.protocol_version to http1.1 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736679878a1947366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论