dfayruzov
@dfayruzov

Python: как поймать исключение в импортируемом модуле?

#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__="dfayruzov"

import mechanize

req = mechanize.urlopen("http://www.ru").read()


dfayruzov@blaster:~/scripts$ ./test.py
Traceback (most recent call last):
File "./test.py", line 8, in req = mechanize.urlopen("http://www.ru").read()
File "build/bdist.linux-x86_64/egg/mechanize/_opener.py", line 426, in urlopen
File "build/bdist.linux-x86_64/egg/mechanize/_opener.py", line 204, in open
File "build/bdist.linux-x86_64/egg/mechanize/_urllib2_fork.py", line 457, in http_response
File "build/bdist.linux-x86_64/egg/mechanize/_opener.py", line 221, in error
File "build/bdist.linux-x86_64/egg/mechanize/_urllib2_fork.py", line 332, in _call_chain
File "build/bdist.linux-x86_64/egg/mechanize/_urllib2_fork.py", line 568, in http_error_302
File "/usr/lib/python2.6/socket.py", line 348, in read
data = self._sock.recv(rbufsize)
File "/usr/lib/python2.6/httplib.py", line 522, in read
return self._read_chunked(amt)
File "/usr/lib/python2.6/httplib.py", line 565, in _read_chunked
raise IncompleteRead(''.join(value))
httplib.IncompleteRead: IncompleteRead(0 bytes read)


#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__="dfayruzov"

import mechanize

try:
req = mechanize.urlopen("http://www.ru").read()
except httplib.IncompleteRead:
print "gotcha!"


dfayruzov@blaster:~/scripts$ ./test.py
Traceback (most recent call last):
File "./test.py", line 9, in except httplib.IncompleteRead:
NameError: name 'httplib' is not defined
  • Вопрос задан
  • 3192 просмотра
Решения вопроса 1
@gaussgs
import mechanize, httplib
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
savados
@savados
Или же

from httplib import IncompleteRead

и потом

except IncompleteRead
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы