D
D
dfayruzov2010-12-18 13:51:31
Python
dfayruzov, 2010-12-18 13:51:31

Python: how to catch an exception in an imported module?

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

import mechanize

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


[email protected]:~/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!"


[email protected]:~/scripts$ ./test.py
Traceback (most recent call last):
File "./test.py", line 9, in except httplib.IncompleteRead:
NameError: name 'httplib' is not defined

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
gaussgs, 2010-12-18
@dfayruzov

import mechanize, httplib

S
savados, 2010-12-18
@savados

Or
from httplib import IncompleteRead
and then
except IncompleteRead

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question