我有一个小项目,将涉及Java应用程序和FlaskWeb服务器之间的重复HTTPS通信,使保持TCP连接活跃是重要的.为了测试和理解这些技术的功能,我设置了一个简单的GET请求生成器,它向我的简单的FlaskWeb服务器应用程序发送多个带有时间倒数的GET请求并返回响应.在这样的设置中,我希望缓存TCP连接,但我认为我的FlaskTM应用程序在每次响应后都会断开连接.请注意,我对Java相当生疏,而且这是我第一次熟悉Flask.
我的Java GET请求生成器如下所示:
package com.deu;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try {
sendHttpRequests("http://127.0.0.1:5000", 1000, 5);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
public static void sendHttpRequests(String url, int interval, int count) throws IOException, InterruptedException {
URL hostURL = new URL(url);
for (int i = 0; i < count; i++) {
HttpURLConnection httpConn = (HttpURLConnection) hostURL.openConnection();
httpConn.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
response.append(line);
System.out.println("Response to i="+i+" "+response.toString());
reader.close();
Thread.sleep(interval);
}
}
}
我的FlaskWeb服务器应用程序如下所示:
import flask
from flask import Flask, request
from werkzeug.serving import WSGIRequestHandler
app = Flask(__name__)
@app.route('/', methods=['GET'])
def hello_world():
resp = flask.Response("Hello World!")
print(request.headers)
print(resp.headers)
return resp
if __name__ == '__main__':
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run()
在Java应用程序终止后,我分别为Java和FlaskTM应用程序获得的输出是:
Response to i=0 Hello World!
Response to i=1 Hello World!
Response to i=2 Hello World!
Response to i=3 Hello World!
Response to i=4 Hello World!
FLASK_APP = app.py
FLASK_ENV = development
FLASK_DEBUG = 0
In folder B:/Development/Projects/Python/ProjectABC
B:\Development\Projects\Python\ProjectABC\venv\Scripts\python.exe -m flask run
* Serving Flask app 'app.py'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
User-Agent: Java/17.0.1
Host: 127.0.0.1:5000
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Type: text/html; charset=utf-8
Content-Length: 12
127.0.0.1 - - [08/Apr/2023 01:26:28] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [08/Apr/2023 01:26:29] "GET / HTTP/1.1" 200 -
User-Agent: Java/17.0.1
Host: 127.0.0.1:5000
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Type: text/html; charset=utf-8
Content-Length: 12
User-Agent: Java/17.0.1
Host: 127.0.0.1:5000
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Type: text/html; charset=utf-8
Content-Length: 12
127.0.0.1 - - [08/Apr/2023 01:26:30] "GET / HTTP/1.1" 200 -
User-Agent: Java/17.0.1
Host: 127.0.0.1:5000
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Type: text/html; charset=utf-8
Content-Length: 12
127.0.0.1 - - [08/Apr/2023 01:26:31] "GET / HTTP/1.1" 200 -
User-Agent: Java/17.0.1
Host: 127.0.0.1:5000
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Type: text/html; charset=utf-8
Content-Length: 12
127.0.0.1 - - [08/Apr/2023 01:26:32] "GET / HTTP/1.1" 200 -
可以看到,请求接收到的响应与预期一致,甚至连接收到的请求标头FlaskTM应用程序都显示为Connection: keep-alive
.但我认为每次都会创建一个新的TCP连接的原因是,响应头不包含Connection: keep-alive
和netstat
cmd命令的输出:
Active Connections
Proto Local Address Foreign Address State
TCP 127.0.0.1:5000 DESKTOP-XXXXXXX:52821 TIME_WAIT
TCP 127.0.0.1:5000 DESKTOP-XXXXXXX:52823 TIME_WAIT
TCP 127.0.0.1:5000 DESKTOP-XXXXXXX:52824 TIME_WAIT
TCP 127.0.0.1:5000 DESKTOP-XXXXXXX:52830 TIME_WAIT
TCP 127.0.0.1:5000 DESKTOP-XXXXXXX:52831 TIME_WAIT
如果我对输出的理解是正确的(如果我错了,请纠正我),建立了5个不同的TCP连接,现在操作系统正在等待清除它们,表明keep-alive
个失败.我在其他问题上搜索答案,试着回答WSGIRequestHandler.protocol_version = "HTTP/1.1"
,但无济于事.
注意:我正在try 先使用HTTP实现持久连接,然后再将其应用于HTTPS.