473,480 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

urllib2 and HTTPBasicAuthHandler

Hi all,
I started to use urllib2 library and HTTPBasicAuthHandler class in order
to authenticate with a http server (Zope in this case).
I don't know why but it doesn't work, while authenticating with direct
headers manipulation works fine!

WinXP Sp2
Python 2.4.4

Thanks in advance for your help/comments

# TestAuthHandler.py
import base64, httplib, urllib2
httplib.HTTPConnection.debuglevel = 1
#
def TestAuth(method):
data =
{'prot':'http','user':'admin','pass':'xxxxxx','hos t':'localhost','port':'8080','path':'manage','real m':'Zope'}
url = '%(prot)s://%(host)s:%(port)s/%(path)s' % data
req = urllib2.Request(url)
#
if (method == 'headers'):
base64string = base64.encodestring('%s:%s' % (data['user'],
data['pass']))[:-1]
req.add_header("Authorization", "Basic %s" % base64string)
elif (method == 'handler'):
auth_url = '%(host)s:%(port)s/%(path)s' % data
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(data['realm'], auth_url, data['user'],
data['pass'])
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
#
f = urllib2.urlopen(req)
data = f.read()
print data
#
TestAuth('headers')
TestAuth('handler')
-------------------------
output:
-------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Zope on http://localhost:8080</title>
</head>
.... all right page ...
</html>
Traceback (most recent call last):
File "C:\Test\TestAuthHandler.py", line 25, in ?
TestAuth('handler')
File "C:\Test\TestAuthHandler.py", line 20, in TestAuth
f = urllib2.urlopen(req)
File "C:\Python\Lib\urllib2.py", line 130, in urlopen
return _opener.open(url, data)
File "C:\Python\Lib\urllib2.py", line 364, in open
response = meth(req, response)
File "C:\Python\Lib\urllib2.py", line 471, in http_response
response = self.parent.error(
File "C:\Python\Lib\urllib2.py", line 402, in error
return self._call_chain(*args)
File "C:\Python\Lib\urllib2.py", line 337, in _call_chain
result = func(*args)
File "C:\Python\Lib\urllib2.py", line 480, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized

Process "Python" completed with Exit Code 1, at 16/01/2007 16:51:18.
Jan 16 '07 #1
3 10165
"m.banaouas" <ba***************@wanadoo.frwrote:
Hi all,
I started to use urllib2 library and HTTPBasicAuthHandler class
in order to authenticate with a http server (Zope in this case).
I don't know why but it doesn't work, while authenticating with
direct headers manipulation works fine!
....
port':'8080','path':'manage','realm':'Zope'}
url = '%(prot)s://%(host)s:%(port)s/%(path)s' % data
....

Are you certain that the realm on the server matches 'Zope'?

HTTPBasicAuthHandler uses the realm to look for the password, if the
realm the server provides is different, urllib2 will respond as if it
does not have a user/password.

If you are only doing one request, it isn't that big a deal; if you
are doing many requests or want urllib2 to handle the response for
other reseans, you can use a default realm, see the bottom of this
example:

http://aspn.activestate.com/ASPN/Coo.../Recipe/305288

(The advantage of using a default rather than making sure the realm is
correct is that it can change and you won't have to do anything)

max

Jan 16 '07 #2
Max,
I verified and determined that Zope realm is "Zope", by tcp spy tool
witch shows me the server headers.
Actually, I tried differents ways without success.
It seems like it's not so easy to negociate a basic authentication
process with urllib2, while it's based on very simple specifications,
however urllib2 is considered as one of the most powerfull library in
its category.

You suggest to use default realm:
# this creates a password manager
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
# because we have put None at the start it will always use this
username/password combination
passman.add_password(None, auth_url, data['user'] , data['pass'])
# create the AuthHandler
auth_handler = urllib2.HTTPBasicAuthHandler(passman)
# build an 'opener' using the handler we've created
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
req = urllib2.Request(url)
f = urllib2.urlopen(req)
data = f.read()
print data
but it doesn't work: urllib2.HTTPError: HTTP Error 401: Unauthorized

Max Erickson a écrit :
"m.banaouas" <ba***************@wanadoo.frwrote:
>Hi all,
I started to use urllib2 library and HTTPBasicAuthHandler class
in order to authenticate with a http server (Zope in this case).
I don't know why but it doesn't work, while authenticating with
direct headers manipulation works fine!
...
>port':'8080','path':'manage','realm':'Zope'}
url = '%(prot)s://%(host)s:%(port)s/%(path)s' % data
...

Are you certain that the realm on the server matches 'Zope'?

HTTPBasicAuthHandler uses the realm to look for the password, if the
realm the server provides is different, urllib2 will respond as if it
does not have a user/password.

If you are only doing one request, it isn't that big a deal; if you
are doing many requests or want urllib2 to handle the response for
other reseans, you can use a default realm, see the bottom of this
example:

http://aspn.activestate.com/ASPN/Coo.../Recipe/305288

(The advantage of using a default rather than making sure the realm is
correct is that it can change and you won't have to do anything)

max
Jan 16 '07 #3
"m.banaouas" <ba***************@wanadoo.frwrote:

....
passman.add_password(None, auth_url, data['user'] ,
....
The only thing I can come up with is that auth_url can't begin with a
protocol, like http:// or whatever.
max

Jan 17 '07 #4

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

Similar topics

4
21075
by: O. Koch | last post by:
Until now, i know that ftplib doesn't support proxies and that i have to use urllib2. But i don't know how to use the urllib2 correct. I found some examples, but i don't understand them. Is...
2
34966
by: Andre Bocchini | last post by:
I'm having some trouble using proxy authentication. I can't figure out how to authenticate with a Squid proxy. I know for a fact the proxy is using Basic instead of Digest for the authentication....
4
4436
by: bmiras | last post by:
I've got a problem using urllib2 to get a web page. I'm going through a proxy using user/password authentification and i'm trying to get a page asking for a HTTP authentification. And I'm using...
5
7373
by: Pascal | last post by:
Hello, I want to acces my OWA (Outlook Web Acces - http Exchange interface) server with urllib2 but, when I try, I've always a 401 http error. Can someone help me (and us)? Thanks. ...
1
3755
by: Fuzzyman | last post by:
I'm trying to do Basic authentication using urllib2 and HTTPPasswordMgr Objects. According to my understanding I ought to perform the following simple sequence (except it doesn't work). Can...
1
3347
by: Ray Slakinski | last post by:
Hello, I have defined a function to set an opener for urllib2, this opener defines any proxy and http authentication that is required. If the proxy has authencation itself and requests an...
2
6236
by: mrstephengross | last post by:
I'm working on learning how to use urllib2 to use a proxy server. I've looked through the postings on this group, and it's been helpful. I have not, however, found complete documentation on the...
1
5731
by: Alessandro Fachin | last post by:
I write this simply code that should give me the access to private page with htaccess using a proxy, i don't known because it's wrong... import urllib,urllib2 #input url...
4
2112
by: Brendan | last post by:
Is there any way to resume an https file download using urllib2 and an HTTPBasicAuthHandler?
0
7046
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
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...
0
7048
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,...
0
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6741
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...
0
6956
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...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.