urllib2 pinger : insight as to use, cause of hang-up?
EP
EP at zomething.com
Mon Jun 6 02:42:00 EDT 2005
"Mahesh" advised:
>
> Timing it out will probably solve it.
>
Thanks.
Follow-on question regarding implementing a timeout for use by urllib2. I am guessing the simplest way to do this is via socket.setdefaulttimeout(), but I am not sure if this sets a global parameter, and if so, whether it might be reset via instantiations of urllib, urllib2, httplib, etc. I assume socket and the timeout parameter is in the global namespace and that I can just reset it at will for application to all the socket module 'users'. Is that right?
(TIA)
[experimenting]
>>> import urllib2plus
>>> urllib2plus.setSocketTimeOut(1)
>>> urllib2plus.urlopen('http://zomething.com')
Traceback (most recent call last):
File "<pyshell#52>", line 1, in -toplevel-
urllib2plus.urlopen('http://zomething.com')
File "C:\Python24\lib\urllib2plus.py", line 130, in urlopen
return _opener.open(url, data)
File "C:\Python24\lib\urllib2plus.py", line 361, in open
response = self._open(req, data)
File "C:\Python24\lib\urllib2plus.py", line 379, in _open
'_open', req)
File "C:\Python24\lib\urllib2plus.py", line 340, in _call_chain
result = func(*args)
File "C:\Python24\lib\urllib2plus.py", line 1024, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python24\lib\urllib2plus.py", line 999, in do_open
raise URLError(err)
URLError: <urlopen error timed out>
>>> urllib2plus.setSocketTimeOut(10)
>>> urllib2plus.urlopen('http://zomething.com')
<addinfourl at 12449152 whose fp = <socket._fileobject object at 0x00BE1340>>
>>> import socket
>>> socket.setdefaulttimeout(0)
>>> urllib2plus.urlopen('http://zomething.com')
Traceback (most recent call last):
File "<pyshell#60>", line 1, in -toplevel-
urllib2plus.urlopen('http://zomething.com')
File "C:\Python24\lib\urllib2plus.py", line 130, in urlopen
return _opener.open(url, data)
File "C:\Python24\lib\urllib2plus.py", line 361, in open
response = self._open(req, data)
File "C:\Python24\lib\urllib2plus.py", line 379, in _open
'_open', req)
File "C:\Python24\lib\urllib2plus.py", line 340, in _call_chain
result = func(*args)
File "C:\Python24\lib\urllib2plus.py", line 1024, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python24\lib\urllib2plus.py", line 999, in do_open
raise URLError(err)
URLError: <urlopen error (10035, 'The socket operation could not complete without blocking')>
>>> socket.setdefaulttimeout(1)
>>> urllib2plus.urlopen('http://zomething.com')
<addinfourl at 12449992 whose fp = <socket._fileobject object at 0x00BE1420>>
More information about the Python-list
mailing list