質問

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
役に立ちましたか?

解決

The old urllib library itself does not support setting a time out, but urllib2 does. You should look into using urllib2, that said...

You can import socket and set a default timeout on all newly created sockets. This might eb a roundabout way to do what you need to do.

http://docs.python.org/2.7/library/socket.html#socket.setdefaulttimeout

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top