473,574 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

urllib2 and HTTPBasicAuthHa ndler

Hi all,
I started to use urllib2 library and HTTPBasicAuthHa ndler 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.HTTPCon nection.debugle vel = 1
#
def TestAuth(method ):
data =
{'prot':'http', 'user':'admin', 'pass':'xxxxxx' ,'host':'localh ost','port':'80 80','path':'man age','realm':'Z ope'}
url = '%(prot)s://%(host)s:%(port )s/%(path)s' % data
req = urllib2.Request (url)
#
if (method == 'headers'):
base64string = base64.encodest ring('%s:%s' % (data['user'],
data['pass']))[:-1]
req.add_header( "Authorization" , "Basic %s" % base64string)
elif (method == 'handler'):
auth_url = '%(host)s:%(por t)s/%(path)s' % data
auth_handler = urllib2.HTTPBas icAuthHandler()
auth_handler.ad d_password(data['realm'], auth_url, data['user'],
data['pass'])
opener = urllib2.build_o pener(auth_hand ler)
urllib2.install _opener(opener)
#
f = urllib2.urlopen (req)
data = f.read()
print data
#
TestAuth('heade rs')
TestAuth('handl er')
-------------------------
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\TestAu thHandler.py", line 25, in ?
TestAuth('handl er')
File "C:\Test\TestAu thHandler.py", line 20, in TestAuth
f = urllib2.urlopen (req)
File "C:\Python\Lib\ urllib2.py", line 130, in urlopen
return _opener.open(ur l, 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.err or(
File "C:\Python\Lib\ urllib2.py", line 402, in error
return self._call_chai n(*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_defa ult
raise HTTPError(req.g et_full_url(), code, msg, hdrs, fp)
urllib2.HTTPErr or: HTTP Error 401: Unauthorized

Process "Python" completed with Exit Code 1, at 16/01/2007 16:51:18.
Jan 16 '07 #1
3 10179
"m.banaouas " <ba************ ***@wanadoo.frw rote:
Hi all,
I started to use urllib2 library and HTTPBasicAuthHa ndler 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','p ath':'manage',' realm':'Zope'}
url = '%(prot)s://%(host)s:%(port )s/%(path)s' % data
....

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

HTTPBasicAuthHa ndler 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.HTTPPas swordMgrWithDef aultRealm()
# because we have put None at the start it will always use this
username/password combination
passman.add_pas sword(None, auth_url, data['user'] , data['pass'])
# create the AuthHandler
auth_handler = urllib2.HTTPBas icAuthHandler(p assman)
# build an 'opener' using the handler we've created
opener = urllib2.build_o pener(auth_hand ler)
urllib2.install _opener(opener)
req = urllib2.Request (url)
f = urllib2.urlopen (req)
data = f.read()
print data
but it doesn't work: urllib2.HTTPErr or: HTTP Error 401: Unauthorized

Max Erickson a écrit :
"m.banaouas " <ba************ ***@wanadoo.frw rote:
>Hi all,
I started to use urllib2 library and HTTPBasicAuthHa ndler 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'?

HTTPBasicAuthHa ndler 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.frw rote:

....
passman.add_pas sword(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
21094
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 there anyone who can help me?
2
34982
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. I can authenticate just fine using Mozilla. I've done some Google searches, but the closest piece of code I've is is for HTTPBasicAuthHandler: ...
4
4450
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 python 2.3 Here is an exemple of the piece of code I use: import urllib2 #Proxy handler proxy_handler = urllib2.ProxyHandler({"http" :
5
7387
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. here's my pyscript:
1
3762
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 someone cast their eyes over it and see what I'm doing wrong please............. import urllib2 theurl = 'http://www.somerestrictedserver.com'
1
3358
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 authenticated file I get a HTTP status code of 401 (Unauthorized access of the file being requested) I do see in the headers the Proxy-authorization and...
2
6251
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 add_password() functions. Here's what I've got so far: #======================================== import urllib2 url = 'http://some.server/form.php'...
1
5743
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 url="http://localhost/private/file" #proxy set up
4
2119
by: Brendan | last post by:
Is there any way to resume an https file download using urllib2 and an HTTPBasicAuthHandler?
0
7814
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7737
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...
0
8253
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7827
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...
0
8109
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...
0
5316
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3752
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...
0
3762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2253
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 we have to send another system

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.