472,778 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Proxy Authentication using urllib2

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:

# set up authentication info
authinfo = urllib2.HTTPBasicAuthHandler()
authinfo.add_password('realm', 'host', 'username', 'password')
proxy_support = urllib2.ProxyHandler({"http" : "http://ahad-haam:3128"})
# build a new opener that adds authentication and caching FTP handlers
opener = urllib2.build_opener(proxy_support, authinfo,
urllib2.CacheFTPHandler)
# install it
urllib2.install_opener(opener)
f = urllib2.urlopen('http://www.python.org/')

Can anybody point me in the right direction to some more detailed
documentation? I haven't really been able to understand what I found
in the Python Library Reference either.

I thought something like this might work, but is didn't:

proxy_handler = urllib2.ProxyHandler({"http" : "http://myproxy:3128"})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
proxy_auth_handler.add_password(None, "myproxy", "myname", "mypass")
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
urllib2.install_opener(opener)
f = urllib2.urlopen("http://www.python.org")
data = f.readlines()

I always get a 407 error.

Well, thanks in advance.

-- Andre
Jul 18 '05 #1
2 34801
Andre Bocchini <li***@andrebocchini.com> writes:
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: [...] proxy_handler = urllib2.ProxyHandler({"http" : "http://myproxy:3128"})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
proxy_auth_handler.add_password(None, "myproxy", "myname", "mypass")
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
urllib2.install_opener(opener)
f = urllib2.urlopen("http://www.python.org")
data = f.readlines()
Can't see anything wrong with that.

I always get a 407 error.


Are you sure you don't need a realm? Try sniffing what Mozilla and
Python are sending (using eg. ethereal). You *should* get a 407
response, IIRC, but then urllib2 should respond with an appropriate
Proxy-authorization header.

If that helped, please look at the doc patch here

http://www.python.org/sf/798244
test it, and post a comment to say whether or not it worked (and which
examples you tried -- preferably all of them ;).
John
Jul 18 '05 #2
Andre Bocchini <li***@andrebocchini.com> wrote in message news:<ma**********************************@python. org>...
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


A piece of code that works through a Squid proxy here - originally
clipped from comp.lang.python - possibly an eff-bot post if my dim
memory serves me correctly.

---- snip ----
import urllib2

proxy_info = {
'user' : 'username',
'pass' : 'password',
'host' : "proxy.name.com",
'port' : 80 # or 8080 or whatever
}

# build a new opener that uses a proxy requiring authorization
proxy_support = urllib2.ProxyHandler({"http" : \
"http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info})
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)

# install it
urllib2.install_opener(opener)

# use it
f = urllib2.urlopen('http://www.python.org/')
print f.headers
print f.read()
---- snip ----

Regards, Myles.
Jul 18 '05 #3

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

Similar topics

4
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...
1
by: Herman Geldenhuys | last post by:
Hey. Im trying to post some variables to an URL, but I get an error saying that my XML is parsed incorrectly. I know that it is not. At closer inspection I found out that some module in Python...
0
by: Carl Waldbieser | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** When using urllib2.urlopen() on my Windows 2000 machine at work, I was puzzled that I kept getting back authentication errors from...
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...
2
by: Licheng Fang | last post by:
I use a HTTP proxy to connect to Internet. When I run ulropen command I get HTTP Error 407: Proxy authorization required. Could anybody tell me how to resolve this? Thanks!
4
by: looping | last post by:
Hi, I have to make internet connections through an ISA proxy server that use NTLM or Kerberos authorization method. I've found a program in python called ntlmaps that act like a proxy and could...
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...
6
by: Jack | last post by:
I'm trying to use a proxy server with urllib2. So I have managed to get it to work by setting the environment variable: export HTTP_PROXY=127.0.0.1:8081 But I wanted to set it from the code....
6
by: Larry Hale | last post by:
Greetings, Pythonistas! My employer has a Squid Proxy between my Python programs and The Internet. I've searched high-and-low, and can only find examples online of how to do basic...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.