Am I misusing socket.setdefaulttimeout here?
Jp Calderone
exarkun at intarweb.us
Thu Mar 20 13:54:34 EST 2003
On Thu, Mar 20, 2003 at 11:57:17AM -0600, Skip Montanaro wrote:
> Socket timeouts are available in 2.3. I'm trying to use them to check URLs
> on a couple web servers without hanging indefinitely due to downed servers:
>
> #!/usr/bin/env python
>
> import socket
> import urllib2
> import sys
>
> TIMEOUT = 30.0
> socket.setdefaulttimeout(TIMEOUT)
>
> f = urllib2.urlopen("http://www.mojam.com/")
> data = f.read()
>
> When I execute the above, it returns immediately (that is, not 30 seconds
> later) with this traceback:
>
> Traceback (most recent call last):
> File "chksrvrs.py", line 11, in ?
> data = f.read()
> File "/Users/skip/local/lib/python2.3/httplib.py", line 1155, in read
> return s + self._file.read()
> IOError: [Errno 35] Resource temporarily unavailable
>
> Do I misunderstand something about socket timeouts or do they not play well
> with urllib and friends (I tried both urllib and urllib2).
>
> Thx,
>
from twisted.web import client
from twisted.internet import defer, reactor
TIMEOUT = 30.0
URL_LIST = ['http://www.mojam.com/']
dList = [client.getPage(u) for u in URL_LIST]
for d in dList:
d.setTimeout(TIMEOUT)
d = defer.DeferredList(dList)
pages = []
d.addCallback(pages.extend).addCallback(lambda R: reactor.stop())
reactor.run()
print pages # Or whatever
> Skip
>
Jp
>
> --
> http://mail.python.org/mailman/listinfo/python-list
--
Examinations are formidable even to the best prepared, for
even the greatest fool may ask more the the wisest man can answer.
-- C.C. Colton
--
up 13:58, 4 users, load average: 0.55, 0.57, 0.47
More information about the Python-list
mailing list