客户端可以通过除 8080 之外的所有端口联系服务器

问题描述 投票:0回答:1

我有一个客户端-服务器 python 项目。我正在使用 fastapi 和 uvicorn。当我使用请求库或邮递员向服务器发出请求时,我得到

ConnectionResetError
。这仅发生在端口 8080 上,而不发生在我尝试过的任何其他端口上。自从我一个月前开始处理我的项目以来,它一直在使用端口 8080。

我使用基本服务器进行了测试

from fastapi import FastAPI

# Create an instance of the FastAPI class
app = FastAPI()

# Define a route for the root endpoint "/"
@app.get("/")
async def read_root():
    i = 1
    return {"message": "Hello, world!"}

# Run the FastAPI application using uvicorn server on port 8080
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8080)

还有这位客户

import requests
response = requests.get(f"http://localhost:8080/")
if response.ok:
    print("No issues")
else:
    print(f"Some issue {response.text}")

这是我得到的错误:

Exception has occurred: ConnectionError
('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))
ConnectionResetError: \[Errno 54\] Connection reset by peer

During handling of the above exception, another exception occurred:

urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

File "/Users/SP/Documents/VSWorkspace/storage/testing/c1.py", line 2, in \<module\>
response = requests.get(f"http://localhost:8080/")
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

我用 lsof -i:8080 检查是否有任何东西正在运行,但什么也没有运行。然而,我确实看到了这一点:

webfilter 26541       root   25u  IPv6 0x5c28b320858b3bef      0t0  TCP localhost:57161->localhost:http-alt (CLOSED) 
如果信息缺失,请告诉我,我会添加。 感谢您的帮助。

python python-requests fastapi uvicorn
1个回答
0
投票

我按照提供的方式运行了您的代码,没有任何问题。也许您想尝试关闭所有终端,以便没有其他 FastAPI 服务器实例在端口 8080 上运行。或者甚至尝试重新启动并查看问题是否仍然存在。

© www.soinside.com 2019 - 2024. All rights reserved.