I have a question on Python 2.7. The urlopen
function sometimes fails to connect due to the bad internet. So I want to add a timer on it, so that when the connection times out, it close the previous connection and try to reconnect again. Here in my implementation, it will fall into a dead loop, to "Trying to reconnect" again and again... Could anyone help me with that? Thank you very much!
def getConnection(urlItem, keywords, address):
urlItem.close()
print "Trying to reconnect..."
findArticles(keywords, address)
def findArticles(keywords, address):
urlItem = urllib.urlopen(address)
t = Timer(20.0, getConnection(urlItem, keywords, address))
t.start()
htmlSource = urlItem.read()
t.cancel()
Here is the exception I got:
File "/usr/lib/python2.7/urllib.py", line 84, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 205, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 342, in open_http
h.endheaders(data)
File "/usr/lib/python2.7/httplib.py", line 951, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 811, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 773, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 754, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno -2] Name or service not known