Score:0

Windows 11, localhost / 127.0.0.1 port number changes on invocation

mz flag

I'm testing a fastapi http server on Windows 11 running on 127.0.0.1:8000. In another terminal, a request is sent to the server on 127.0.0.1:8000. The result is a Bad Request and the server says it happened on 127.0.0.1:50340. The port number changed from 8000 to 50340. Re-running the query again will change the port to a different number again. Details are in the outputs:

The Server output is:

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.

The Client output is:

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

The Server output after the Client request is:

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

Test 127.0.0.1:8000 works

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

I've Network Reset and performed the usual network reset operations but with no luck. Anyone know why this is happening and what the solution is?

vidarlo avatar
ar flag
The webserver shows the *clients* port number. This is commonly known as [ephemeral ports](https://en.wikipedia.org/wiki/Ephemeral_port). This is *not* the cause of any problems you observe. It's completely normal and expected.
Henry Thornton avatar
mz flag
Don't understand. The server output says the fastapi/uvicorn server is running on 127.0.0.1:8000. Or, are you talking about something else?
vidarlo avatar
ar flag
It is. TCP connections however have a client and a server. Both parties use ports to differentiate between connections. What you see in your logs is the *client* port number. Anyway, this question is off topic on serverfault.
Henry Thornton avatar
mz flag
Re: Anyway, this question is off topic on serverfault. Oh :( Where do I ask?
vidarlo avatar
ar flag
It's probably good on superuser.com.
Henry Thornton avatar
mz flag
Ok. Will post on superuser. Thanks
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.