tags:

views:

49

answers:

1

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 which requires no timeout.

Is there an easy way to do this or is this going to be a difficult thing to do?

Does python allow a reset of the timeout after connected so that I can use makefile and still have a timeout for the socket connection

A: 

You just need to the socket settimeout() method before attempting the connect(), please note that after connecting you must settimeout(None) to set the socket into blocking mode, such is required for the makefile . Here is the code I am using:

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.settimeout(10)
socket.connect(address)
socket.settimeout(None)
fileobj = socket.makefile('rb', 0)
João Pinto