473,395 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Urllib2 / add_password method

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'

proxy_url = 'http://202.62.252.3:8080' # A publicly available anonymous
proxy server

proxy_handler = urllib2.ProxyHandler( {'http': proxy_url } )

proxy_auth_handler = urllib2.HTTPBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username',
'password')

opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)

urllib2.install_opener(opener)

try:
handler = urllib2.urlopen(url)
except urllib2.URLError:
print "whoops!"
else:
print handler.read()
#=======================================

It works, but I don't really know what I'm doing with the
proxy_auth_handler part. Specifying username and password make sense,
but I haven't found documentation on what 'realm' and 'host' are for.
Shouldn't username & password be sufficient?

Thanks,
--Steve (mr************@hotmail.com)

Apr 28 '06 #1
2 6232
"mrstephengross" <mr************@hotmail.com> writes:
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:
Don't use a password manager with proxy auth in 2.4, it doesn't work:

http://python.org/sf/1470846

[...] It works, but I don't really know what I'm doing with the
proxy_auth_handler part. Specifying username and password make sense,
but I haven't found documentation on what 'realm' and 'host' are for.
Shouldn't username & password be sufficient?


Both proxy and auth handling in urllib2 are buggy in 2.4, but passing
the username and password in the "userinfo" component of the URL works
(didn't read your code, hope nothing important in there):

import urllib2

proxy_handler = urllib2.ProxyHandler({
"http": "http://john:pa******@proxy.example.com:3128"})
opener = urllib2.build_opener(proxy_handler)

response = opener.open(url)
However, ISTR specifying a port number as I do above (the ":3128" bit)
is not supported in 2.4 (it does in current SVN, so will in 2.5).
Hmm, I notice just now that urllib2's digest auth support breaks
urllib2's handler scheme. Must file another bug...
John

Apr 29 '06 #2
On Sat, 29 Apr 2006, John J. Lee wrote:
"mrstephengross" <mr************@hotmail.com> writes:
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:


Don't use a password manager with proxy auth in 2.4, it doesn't work:

http://python.org/sf/1470846

[...]
It works, but I don't really know what I'm doing with the
proxy_auth_handler part. Specifying username and password make sense,
but I haven't found documentation on what 'realm' and 'host' are for.
Shouldn't username & password be sufficient?


Oops, didn't read your question.

I'm surprised if it worked at all given that bug (maybe you're using an
older Python, and I forget all the details of the bug).

Anyway, to answer your question: yes, username & password *should* be
sufficient, but the password manager classes in urllib2 at present aren't
very friendly like that. You can use HTTPPasswordMgrWithDefaultRealm so
that at least you can forget about the realm part. (The "realm" is what
you see in the title bar of the pop-up that web browsers display. See RFC
2617 for more details.)

I'm adding friendlier proxy/auth support to package mechanize ATM (and
finding/fixing bugs as I go), so maybe some of that will find its way into
Python 2.6.
John

Apr 29 '06 #3

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

Similar topics

2
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
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...
1
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...
0
by: Neil.Jin | last post by:
I got some source code form python's help document. the below: proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'}) proxy_auth_handler = urllib2.HTTPBasicAuthHandler()...
1
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...
6
by: Alejandro Dubrovsky | last post by:
I see from googling around that this is a popular topic, but I haven't seen anyone saying "ah, yes, that works", so here it goes. How does one connect through a proxy which requires basic...
3
by: m.banaouas | last post by:
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...
1
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...
1
by: Rob Wolfe | last post by:
"Miguel Beltran R." <yourpadre@gmail.comwrites: You forgot to add the authentication handler to the list of handlers. See below. Add here: opener.add_handler(aut))
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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...

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.