Python socket connection timeout

后端 未结 3 1959
忘了有多久
忘了有多久 2020-11-30 00:45

I have a socket that I want to timeout when connecting so that I can cancel the whole operation if it can\'t connect yet it also want to use the makefile for the socket whic

3条回答
  •  猫巷女王i
    2020-11-30 01:49

    If you are using Python2.6 or newer, it's convenient to use socket.create_connection

    sock = socket.create_connection(address, timeout=10)
    sock.settimeout(None)
    fileobj = sock.makefile('rb', 0)
    

提交回复
热议问题