Функция urllib2.urlopen
зависает. Поэтому мой вопрос прост:
- Почему
urlopen
останавливает мой скрипт навсегда, даже если время ожидания установлено?
- Как я могу получить доступ к данным по URL (в данном случае: http://api.own3d.tv/live?channel=FnaticTV) без возможности зависания моего процесса Python на всю вечность?
Это та часть, где зависает (In own3d.py ):
# Try three times to make contact
while True:
try:
# Connect to API
# Right here! It freezes here
connection = urllib2.urlopen(request, timeout=10)
xmlstring = connection.read()
except URLError as e:
tries += 1
if tries >= 3:
sys.stderr.write(
'own3dStreamsUpdater: Fatal error: Repeated timeouts')
exit()
Это трассировка стека после моего KeyboardInterrupt
Traceback (most recent call last):
File "", line 1, in
File "honsapp/own3dStreamsUpdater.py", line 53, in updateStreamInfo
streamInfo = getStreamInfo(stream)
File "honsapp/own3d.py", line 98, in getStreamInfo
connection = urllib2.urlopen(request, timeout=10)
File "/usr/local/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/local/lib/python2.7/urllib2.py", line 394, in open
response = self._open(req, data)
File "/usr/local/lib/python2.7/urllib2.py", line 412, in _open
'_open', req)
File "/usr/local/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/usr/local/lib/python2.7/urllib2.py", line 1199, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/local/lib/python2.7/urllib2.py", line 1170, in do_open
r = h.getresponse(buffering=True)
File "/usr/local/lib/python2.7/httplib.py", line 1027, in getresponse
response.begin()
File "/usr/local/lib/python2.7/httplib.py", line 407, in begin
version, status, reason = self._read_status()
File "/usr/local/lib/python2.7/httplib.py", line 365, in _read_status
line = self.fp.readline()
File "/usr/local/lib/python2.7/socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
Редактировать
Я позволил своему сценарию работать всю ночь. Я не знаю точно, сколько времени это заняло (хотя и больше пяти минут), но сценарий, наконец, сдался и дал мне трассировку стека:
Traceback (most recent call last):
File "honsapp/own3dStreamsUpdater.py", line 260, in
newInfo()
File "honsapp/own3dStreamsUpdater.py", line 172, in newInfo
result = updateStreamInfo(stream)
File "honsapp/own3dStreamsUpdater.py", line 53, in updateStreamInfo
streamInfo = getStreamInfo(stream)
File "/root/Dropbox/Projects/honstreams/honsapp/own3d.py", line 98, in getStreamInfo
connection = urllib2.urlopen(request, timeout=10)
File "/usr/local/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/local/lib/python2.7/urllib2.py", line 394, in open
response = self._open(req, data)
File "/usr/local/lib/python2.7/urllib2.py", line 412, in _open
'_open', req)
File "/usr/local/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/usr/local/lib/python2.7/urllib2.py", line 1199, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/local/lib/python2.7/urllib2.py", line 1170, in do_open
r = h.getresponse(buffering=True)
File "/usr/local/lib/python2.7/httplib.py", line 1027, in getresponse
response.begin()
File "/usr/local/lib/python2.7/httplib.py", line 407, in begin
version, status, reason = self._read_status()
File "/usr/local/lib/python2.7/httplib.py", line 371, in _read_status
raise BadStatusLine(line)
httplib.BadStatusLine: ''