1

when I use HTTPConnection under python 2.4,if the serve do not answer,the connection is keeped forever.How can i break it?

hanerlv
  • 13
  • 3
  • This should help - http://stackoverflow.com/questions/2084782/timeout-for-urllib2-urlopen-in-pre-python-2-6-versions – Puneet Feb 22 '12 at 10:38

3 Answers3

2

you can use socket.setdefaulttimeout() and set it to a global timeout

example :

import socket
socket.setdefaulttimeout(30)

now all httplib/urllib2 requests will have a timeout of 30 seconds.

WeaselFox
  • 7,100
  • 7
  • 42
  • 73
1

For those who use Python 2.7 and later, HTTPConnection provides a timeout parameter !

httplib.HTTPConnection('www.cwi.nl', 80, timeout=10)

(This doesn't answer this question because the OP asked for Python 2.4, but it can help others looking for that specific answer).

Cyril N.
  • 37,442
  • 34
  • 128
  • 228
0

Use that connection in the separate thread and join it using the desired timeout value.

Roman Bodnarchuk
  • 28,019
  • 11
  • 57
  • 73