0

I have a Python script that recursively sends data to a central server. The router will go to sleep until there's new data queued up to be transmitted. The wake up process takes about 20 Seconds or so thus I attempted to establish my connection as follows:

TIMEOUT = 100
def connect():
    return HTTPConnection(HOST, timeout=TIMEOUT)

connection = connect()

Now, I'd expect the function HTTPConnection() to return only once the timeout has expired which isn't what it's doing. My function returns after 3 or so seconds and gives me a connection error. Why is that? Do I need to write my own timeout loop?

Thank you, Ron

stdcerr
  • 13,725
  • 25
  • 71
  • 128
  • What OS are you running this on? –  Feb 01 '13 at 23:10
  • 1
    have you tried setting `socket.setdefaulttimeout` ? – loopbackbee Feb 01 '13 at 23:10
  • Also, is the an IP address or a domain name? If the latter, do you have a DNS server (or cache) inside the router? – abarnert Feb 01 '13 at 23:19
  • Mike, on Linux - does this matter? :o – stdcerr Feb 01 '13 at 23:31
  • @goncalopp, yes I have tried that, doesn't help either... :( – stdcerr Feb 01 '13 at 23:59
  • if `socket.setdefaulttimeout` doesn't work, my guess is that your problem isn't a python problem at all (see @abarnert 's question). It may be useful to include a stack trace – loopbackbee Feb 02 '13 at 00:05
  • @goncalopp is probably right. If you want to be sure, first write a simple Python test program that does a trivial HTTP transaction at the `socket` level and see if it has the same behavior. (At that point, you can be pretty sure that it's not Python… but if you want to be absolutely sure, translate that simple Python test into C and try that. Or, maybe more simply, do it with `bash` and `netcat`…) – abarnert Feb 02 '13 at 00:19

1 Answers1

0

Well, the way I ended up fixing my problem is, in order to wake up the router, I send a ping with a very long timeout to 8.8.8.8 which will wake up the router and get a reply

def wakeup():
    subprocess.Popen(["/bin/ping", "-c2", "-w"+str(WAKETIME), PINGHOST], stdout=subprocess.PIPE).stdout.read()

And I just call this function before i do any other http requests so I don't have to bother with increasing the HTTP timeout at all.

stdcerr
  • 13,725
  • 25
  • 71
  • 128