我的代码是这样的:
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.我做错了什么?
It works fine on the first run, but gives me Errno 9 every time after. What am I doing wrong?
顺便说一句,
mcip = "mau5ville" port = 25565 magic = "xFE" 推荐答案您正在关闭的同一个套接字上调用 connect.你不能那样做.
You're calling connect on the same socket you closed. You can't do that.
至于 文档 关闭 说:
以后对套接字对象的所有操作都将失败.
All future operations on the socket object will fail.
只需将 s = socket.socket()(或任何您拥有的)移动到循环中.(或者,如果您愿意,可以使用 create_connection 而不是分两步做,这使得这更难出错,也意味着您不必猜测 IPv4 与 IPv6 等)
Just move the s = socket.socket() (or whatever you have) into the loop. (Or, if you prefer, use create_connection instead of doing it in two steps, which makes this harder to get wrong, as well as meaning you don't have to guess at IPv4 vs. IPv6, etc.)
更多推荐
获取 Errno 9:python 套接字中的错误文件描述符
发布评论