获取 Errno 9:python 套接字中的错误文件描述符

编程入门 行业动态 更新时间:2024-10-27 20:25:15
本文介绍了获取 Errno 9:python 套接字中的错误文件描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的代码是这样的:

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 套接字中的错误文件描述符

本文发布于:2023-10-08 14:02:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   文件   Errno   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!