0

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?

  • 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. – vidarlo Feb 17 '23 at 15:42
  • 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? – Henry Thornton Feb 17 '23 at 15:46
  • 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. – vidarlo Feb 17 '23 at 15:47
  • Re: Anyway, this question is off topic on serverfault. Oh :( Where do I ask? – Henry Thornton Feb 17 '23 at 15:49
  • It's probably good on superuser.com. – vidarlo Feb 17 '23 at 15:50
  • Ok. Will post on superuser. Thanks – Henry Thornton Feb 17 '23 at 15:51

0 Answers0