473,672 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error Codes From a URLError

I've written a CGI proxy that remotely fetches web pages. I use the
urlopen function in CLientCookie which replaces (and ultimately uses)
the urlopen function in urllib2. What I'd like to do is give a more
useful message to the user when the page fetch fails.

My understanding was that a URLError exception had a 'code' attribute
which was the error code.

I trap the error with a simple : (excuse the horrible indentation,
this is from the middle of a program)

try:
req = urllib2.Request (theurl, txdata, txheaders)
if data['name'] and data['pass']:
import base64
base64string = base64.encodest ring('%s:%s' %
(data['name'], data['pass']))[:-1]
req.add_header( "Authorization" , "Basic %s" %
base64string)
u = openfun(req)
......
except IOError, e: # an error in fetching
the page will raise a URLError which is a subclass of IOError
if not cdone:
print "Content-type: text/html" # this is the
header to the server
print # so is this
blank line
if not hasattr(e, 'code'): # If it's not
an http error - raise it as an error
raise

elif e.code == 401: #
authorisation
print authmess % (theurl, data['mod'])
print authend

elif e.code in errorlist: # standard
http errors
the_err = errorlist[e.code]
print err_mes % (the_err[0], the_err[0], the_err[1])

else: # any others
raise

openfun is urlopen function from ClientCookie (if available) or from
urllib2.

What ought to happen in my limied understanding, is that the exception
(e) should have the attribute 'code' if it a URLError - and that value
ought to tell me what type of error it is... (the error message is
then ina dictionary called errorlist which I nicked from
BaseHTTPServer) .

*However*, when I use it to try to fetch a non-existent page
(expecting a 404 error) - it falls at the first raise statement.
Evidently the exception *is* an IOError, but doesn't have the code
attribute.
*However* - you see the explicit test for a 401 (authentication )
error. I have code that can handle authentication. .. if I go to a page
that requires authentication it picks it up fine...... (so the
URLError in that case does have the code attribute).

The exact error traceback is below - and you can see where urllib2
raises the URLError - because of what it sees as a socket problem...
but it obviously isn't returning the error code properly. Any
suggestions ?

URLError Python 2.2.2: /usr/bin/python
Fri Jul 23 11:06:39 2004

A problem occurred in a Python script. Here is the sequence of
function calls leading up to the error, in the order they occurred.

/home/buildin/public_html/cgi-bin/approx.py
280 print "Content-type: text/html" # this
is the header to the server

281 print # so is
this blank line

282 if not hasattr(e, 'code'): # If
it's not an http error - raise it as an error

283 raise

284

hasattr undefined, e = <urllib2.URLErr or instance>
/home/buildin/public_html/cgi-bin/ClientCookie/_urllib2_suppor t.py in
urlopen(url=<ur llib2.Request instance>, data=None)
969 finally:

970 urlopen_lock.re lease()

971 return _opener.open(ur l, data)

972

973 def urlretrieve(url , filename=None, reporthook=None ,
data=None):

global _opener = <ClientCookie._ urllib2_support .OpenerDirector
instance>, _opener.open = <bound method OpenerDirector. open of
<ClientCookie._ urllib2_support .OpenerDirector instance>>, url =
<urllib2.Reques t instance>, data = None
/home/buildin/public_html/cgi-bin/ClientCookie/_urllib2_suppor t.py in
open(self=<Clie ntCookie._urlli b2_support.Open erDirector instance>,
fullurl=<urllib 2.Request instance>, data=None)
567 req = meth(req)

568

569 response = urllib2.OpenerD irector.open(se lf, req,
data)

570

571 # post-process response

response undefined, global urllib2 = <module 'urllib2' from
'/usr/lib/python2.2/urllib2.pyc'>, urllib2.OpenerD irector = <class
urllib2.OpenerD irector>, urllib2.OpenerD irector.open = <unbound method
OpenerDirector. open>, self =
<ClientCookie._ urllib2_support .OpenerDirector instance>, req =
<ClientCookie._ urllib2_support .Request instance>, data = None
/usr/lib/python2.2/urllib2.py in
open(self=<Clie ntCookie._urlli b2_support.Open erDirector instance>,
fullurl=<Client Cookie._urllib2 _support.Reques t instance>, data=None)
320 type_ = req.get_type()

321 result = self._call_chai n(self.handle_o pen, type_, type_
+ \

322 '_open', req)

323 if result:

324 return result

req = <ClientCookie._ urllib2_support .Request instance>
/usr/lib/python2.2/urllib2.py in
_call_chain(sel f=<ClientCookie ._urllib2_suppo rt.OpenerDirect or
instance>, chain={'file': [<urllib2.FileHa ndler instance>], 'ftp':
[<urllib2.FTPHan dler instance>], 'http':
[<ClientCookie._ urllib2_support .HTTPHandler instance>], 'https':
[<ClientCookie._ urllib2_support .HTTPSHandler instance>], 'unknown':
[<urllib2.Unknow nHandler instance>]}, kind='http',
meth_name='http _open', *args=(<ClientC ookie._urllib2_ support.Request
instance>,))
299 func = getattr(handler , meth_name)

300

301 result = func(*args)

302 if result is not None:

303 return result

result undefined, func = <bound method HTTPHandler.htt p_open of
<ClientCookie._ urllib2_support .HTTPHandler instance>>, args =
(<ClientCookie. _urllib2_suppor t.Request instance>,)
/home/buildin/public_html/cgi-bin/ClientCookie/_urllib2_suppor t.py in
http_open(self= <ClientCookie._ urllib2_support .HTTPHandler instance>,
req=<ClientCook ie._urllib2_sup port.Request instance>)
883 class HTTPHandler(Abs tractHTTPHandle r):

884 def http_open(self, req):

885 return self.do_open(ht tplib.HTTP, req)

886

887 http_request = AbstractHTTPHan dler.do_request _

self = <ClientCookie._ urllib2_support .HTTPHandler instance>,
self.do_open = <bound method HTTPHandler.do_ open of
<ClientCookie._ urllib2_support .HTTPHandler instance>>, global httplib
= <module 'httplib' from '/usr/lib/python2.2/httplib.pyc'>,
httplib.HTTP = <class httplib.HTTP>, req =
<ClientCookie._ urllib2_support .Request instance>
/home/buildin/public_html/cgi-bin/ClientCookie/_urllib2_suppor t.py in
do_open(self=<C lientCookie._ur llib2_support.H TTPHandler instance>,
http_class=<cla ss httplib.HTTP>,
req=<ClientCook ie._urllib2_sup port.Request instance>)
728 h.endheaders()

729 except socket.error, err:

730 raise URLError(err)

731 if req.has_data():

732 h.send(req.get_ data())

global URLError = <class urllib2.URLErro r>, err = <socket.gaierro r
instance>
URLError:
__doc__ = None
__getitem__ = <bound method URLError.__geti tem__ of
<urllib2.URLErr or instance>>
__init__ = <bound method URLError.__init __ of <urllib2.URLErr or
instance>>
__module__ = 'urllib2'
__str__ = <bound method URLError.__str_ _ of <urllib2.URLErr or
instance>>
reason = <socket.gaierro r instance>

Regards,
Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #1
2 4733
On 23 Jul 2004, Fuzzyman wrote:
*However*, when I use it to try to fetch a non-existent page
(expecting a 404 error) - it falls at the first raise statement.
Evidently the exception *is* an IOError, but doesn't have the code
attribute.


For the "non-existent page", are you using a url of the form
'http://fake.server/some/file' or 'http://real.server/fake/file'?
Because if the host is nonexistent (and therefore a connection can't be
opened), then I'd expect urllib2 to raise an IOError. I'd expect an
HTTPError only if the server exists and explicitly returns a 404.

Jul 18 '05 #2
Christopher T King <sq******@WPI.E DU> wrote in message news:<Pi******* *************** *************** *@ccc8.wpi.edu> ...
On 23 Jul 2004, Fuzzyman wrote:
*However*, when I use it to try to fetch a non-existent page
(expecting a 404 error) - it falls at the first raise statement.
Evidently the exception *is* an IOError, but doesn't have the code
attribute.


For the "non-existent page", are you using a url of the form
'http://fake.server/some/file' or 'http://real.server/fake/file'?
Because if the host is nonexistent (and therefore a connection can't be
opened), then I'd expect urllib2 to raise an IOError. I'd expect an
HTTPError only if the server exists and explicitly returns a 404.


Right - that would explain it.
But it makes it difficult to trap the errors - an IOError *might* mean
the page is non-existent... *or* it might mean another problem !!

Oh well.

Thanks

Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4964
by: Matt | last post by:
I'm trying to get the HTML data off of a webpage. Let's say for the sake of argument it's the python homepage. I've googled around and found some examples that people said worked. Here's what I've cobbled together: #getHTML.py ############################################ import urllib import urllib2 proxy_info = {'user':'us3r',
0
2068
by: Benjamin Schollnick | last post by:
Folks, With Windows XP, and Python v2.41 I am running into a problem.... The following code gives me an unknown protocol error.... And I am not sure how to resolve it... I have a API written for DocuShare for a non-SSL server, and I wanted to update it
1
2384
by: joemynz | last post by:
Help please with a URLError. Invoking a url that works in Firefox and IE results in a "urlerror 7, no address ..." in python. I need to debug why. Traceback is below. There's a redirect when the url is invoked (it's part of a chain) - you can see it using liveheaders in firefox. What is the best way to debug this? I tried setting debug on HTTPConnection but this doesn't work (from some web posts, setting debug is broken in 2.4). What's...
0
1011
by: mh121 | last post by:
Hi, I am trying to input a spreadsheet of possible domain names and output the length of the sourcecode of the webpage (if it exists). In testing this, I’ve come across a lot of errors, for example, when websites ask for username/password, take too slow to load, or no longer work. I am a newbie and am looking for help dealing with multiple errors. First, is the basic code setup below the best way to deal with multiple errors? I would...
0
2619
by: Jinshi | last post by:
Hello, everyone, I am quite new to python and I don't know how to solve this problem. I hope someone can help me. I just did a test in python shell: Traceback (most recent call last): File "c:\Zope\<string>", line 1, in ? File "C:\Zope\2.10.5\Python\Lib\urllib2.py", line 130, in urlopen
0
916
by: Penny Y. | last post by:
Hello, I run this small script: import urllib2,sys try: r=urllib2.urlopen("http://un-know-n.com/") except URLError,e: print str(e) sys.exit(1)
1
3820
by: Gabriel Genellina | last post by:
En Tue, 15 Apr 2008 02:54:43 -0300, Penny Y. <pylists@arcor.deescribió: Same as the function urlopen, you have to qualify URLError with the module first: except urllib2.URLError, e: ... Or import both things from urllib2:
2
19460
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
4
5523
by: Mike Driscoll | last post by:
Hi, I have been using the following code for over a year in one of my programs: f = urllib2.urlopen('https://www.companywebsite.com/somestring') It worked great until the middle of the afternoon yesterday. Now I get the following traceback:
0
8419
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8946
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7477
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6260
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4243
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2094
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.