gpt4 book ai didi

python - 获取 Errno 9 : Bad file descriptor in python socket

转载 作者:IT老高 更新时间:2023-10-28 22:17:42 27 4
gpt4 key购买 nike

我的代码是这样的:

while 1:
# Determine whether the server is up or down
try:
s.connect((mcip, port))
s.send(magic)
data = s.recv(1024)
s.close()
print data
except Exception, e:
print e
sleep(60)

它在第一次运行时运行良好,但之后每次都给我 Errno 9。我做错了什么?

顺便说一句,

mcip = "mau5ville.com"
port = 25565
magic = "\xFE"

最佳答案

您在关闭的同一个套接字上调用 connect。你不能那样做。

至于the docs对于 close 说:

All future operations on the socket object will fail.

只需将 s = socket.socket() (或任何你拥有的)移动到循环中。 (或者,如果您愿意,可以使用 create_connection,而不是分两步进行,这样更不容易出错,也意味着您不必猜测 IPv4 与 IPv6 等等。)

关于python - 获取 Errno 9 : Bad file descriptor in python socket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15958026/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com