Heroku Python 套接字连接失败

lol*_*123 5 python sockets heroku

我正在尝试将 python 套接字服务器部署到 Heroku,但似乎套接字连接没有建立得很好...在我的情况下,该accept()方法应该等待建立套接字连接,但似乎它甚至会触发如果我不运行我的client.py脚本...

另外,当运行客户端时,确实建立了连接,但服务器不接收也不发送任何数据......

我究竟做错了什么?所有这些都在本地运行...(将端口从 80 更改为 12345,并将地址更改为 localhost)...

这是我到目前为止所拥有的:

# server.py
import socket
import os

server = socket.socket()
port = int(os.environ.get("PORT", 12345))
server.bind(("0.0.0.0", port))
server.listen(5)

# should wait for an actual connection, but fires right away
s, _ = server.accept()

# should wait to receive "Hello, world! (from client)", but receives b''
s.recv(1024)

s.send("Hello, world! (from server)".encode())

s.close()
Run Code Online (Sandbox Code Playgroud)
# client.py
import socket

s = socket.socket()

print("connecting to server...")
# "...my-heroku-app.." changed to the actual heroku app url
s.connect(("my-heroku-app.herokuapp.com", 80))

print("sending data...")
s.send("Hello, world! (from client)".encode())

print("receiving data...")

# should receive "Hello, world! (from server)",
# but blocks because the server already stopped
print(s.recv(1024).decode())

s.close()
Run Code Online (Sandbox Code Playgroud)
# Procfile
web: python server.py
Run Code Online (Sandbox Code Playgroud)

我还尝试将其放入s.recv()循环中,直到收到的数据不是“无”,但这只是永远循环......(在服务器端)

我在这里做错了什么?服务器绑定(地址和端口)是否正确?Heroku 支持这种服务器吗?我似乎没有找到任何关于如何做到这一点的信息(只有使用 Flask 和其他网络服务器框架的示例......)。

编辑

经过一些记录,这似乎172.18.17.53是始终连接到服务器并保持繁忙的 IP。似乎该 IP 无法访问,因为 send() 函数会阻塞...

我尝试添加s.settimeout(0.05)但它没有改变该send()方法......