Getting Errno 9: Bad file descriptor in python socket

后端 未结 2 1159
面向向阳花
面向向阳花 2020-12-03 04:38

My code is this:

while 1:
    # Determine whether the server is up or down
    try:
        s.connect((mcip, port))
        s.send(magic)
        data = s.re         


        
2条回答
  •  广开言路
    2020-12-03 05:00

    You're calling connect on the same socket you closed. You can't do that.

    As for the docs for close say:

    All future operations on the socket object will fail.

    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.)

提交回复
热议问题