Windows 11,localhost/127.0.0.1 端口号在调用时发生变化

Windows 11,localhost/127.0.0.1 端口号在调用时发生变化

我正在 Windows 11 上测试在 127.0.0.1:8000 上运行的 fastapi http 服务器。在另一个终端中,向 127.0.0.1:8000 上的服务器发送请求。结果是一个错误请求,服务器说它发生在 127.0.0.1:50340 上。端口号从 8000 更改为 50340。再次重新运行查询将再次将端口更改为不同的数字。详细信息在输出中:

服务器输出是:

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [2368] using StatReload
{"event":"logger initialized","level":"info","timestamp":"2023-02-17T14:53:00.373918"}
{"event":"logger initialized","level":"info","timestamp":"2023-02-17T14:53:00.592613"}
INFO:     Started server process [24260]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

客户端输出是:

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1:8000
send: b'POST /api/query HTTP/1.1\r\nHost: 127.0.0.1:8000\r\nUser-Agent: python-requests/2.28.2\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: 160\r\nContent-Type: application/json\r\n\r\n'
send: b'{"request_type": "query", "query_items": ["196313", "9223372032559872706", "262946", "9223372032559842189"], "number_of_predictions": 10, "include_score": true}'
reply: 'HTTP/1.1 404 Not Found\r\n'
header: date: Fri, 17 Feb 2023 14:54:23 GMT
header: server: uvicorn
header: content-length: 76
header: content-type: application/problem+json
DEBUG:urllib3.connectionpool:http://127.0.0.1:8000 "POST /api/query HTTP/1.1" 404 76

客户端请求后服务器输出为:

INFO:     127.0.0.1:50340 - "POST /api/query HTTP/1.1" 404 Not Found

测试 127.0.0.1:8000 是否有效

PS C:\Windows\System32> Test-NetConnection 127.0.0.1 -p 8000
ComputerName     : 127.0.0.1
RemoteAddress    : 127.0.0.1
RemotePort       : 8000
InterfaceAlias   : Loopback Pseudo-Interface 1
SourceAddress    : 127.0.0.1
TcpTestSucceeded : True

我已经重置了网络,并执行了常规的网络重置操作,但没有成功。有人知道为什么会发生这种情况以及解决方案是什么吗?

相关内容