Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What errors/exceptions do I need to handle with urllib2.Request / urlopen?

Tags:

python

I have the following code to do a postback to a remote URL:

request = urllib2.Request('http://www.example.com', postBackData, { 'User-Agent' : 'My User Agent' })  try:      response = urllib2.urlopen(request) except urllib2.HTTPError, e:     checksLogger.error('HTTPError = ' + str(e.code)) except urllib2.URLError, e:     checksLogger.error('URLError = ' + str(e.reason)) except httplib.HTTPException, e:     checksLogger.error('HTTPException') 

The postBackData is created using a dictionary encoded using urllib.urlencode. checksLogger is a logger using logging.

I have had a problem where this code runs when the remote server is down and the code exits (this is on customer servers so I don't know what the exit stack dump / error is at this time). I'm assuming this is because there is an exception and/or error that is not being handled. So are there any other exceptions that might be triggered that I'm not handling above?

like image 793
davidmytton Avatar asked Mar 20 '09 12:03

davidmytton


People also ask

What does Urllib request Urlopen do?

request is a Python module for fetching URLs (Uniform Resource Locators). It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different protocols.

What does Urllib Urlopen return?

The data returned by urlopen() or urlretrieve() is the raw data returned by the server. This may be binary data (such as an image), plain text or (for example) HTML. The HTTP protocol provides type information in the reply header, which can be inspected by looking at the Content-Type header.

Which module defines the classes for exception raised by Urllib request?

error module defines the exception classes for exceptions raised by urllib. request . The base exception class is URLError . The handlers raise this exception (or derived exceptions) when they run into a problem.

What does Urlopen mean?

So in layman terms urlopen() opens a connection to the url and response.


1 Answers

Add generic exception handler:

request = urllib2.Request('http://www.example.com', postBackData, { 'User-Agent' : 'My User Agent' })  try:      response = urllib2.urlopen(request) except urllib2.HTTPError, e:     checksLogger.error('HTTPError = ' + str(e.code)) except urllib2.URLError, e:     checksLogger.error('URLError = ' + str(e.reason)) except httplib.HTTPException, e:     checksLogger.error('HTTPException') except Exception:     import traceback     checksLogger.error('generic exception: ' + traceback.format_exc()) 
like image 167
vartec Avatar answered Sep 23 '22 20:09

vartec