获取 Errno 9:python 套接字中的错误文件描述符 [英] Getting Errno 9: Bad file descriptor in python socket

查看:26
本文介绍了获取 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.com"
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 套接字中的错误文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆