473,789 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 HTTPBasicAuthHa ndler:

# set up authentication info
authinfo = urllib2.HTTPBas icAuthHandler()
authinfo.add_pa ssword('realm', 'host', 'username', 'password')
proxy_support = urllib2.ProxyHa ndler({"http" : "http://ahad-haam:3128"})
# build a new opener that adds authentication and caching FTP handlers
opener = urllib2.build_o pener(proxy_sup port, authinfo,
urllib2.CacheFT PHandler)
# 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.ProxyHa ndler({"http" : "http://myproxy:3128"})
proxy_auth_hand ler = urllib2.ProxyBa sicAuthHandler( )
proxy_auth_hand ler.add_passwor d(None, "myproxy", "myname", "mypass")
opener = urllib2.build_o pener(proxy_han dler, proxy_auth_hand ler)
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 34996
Andre Bocchini <li***@andreboc chini.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 HTTPBasicAuthHa ndler: [...] proxy_handler = urllib2.ProxyHa ndler({"http" : "http://myproxy:3128"})
proxy_auth_hand ler = urllib2.ProxyBa sicAuthHandler( )
proxy_auth_hand ler.add_passwor d(None, "myproxy", "myname", "mypass")
opener = urllib2.build_o pener(proxy_han dler, proxy_auth_hand ler)
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***@andreboc chini.com> wrote in message news:<ma******* *************** ************@py thon.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.pytho n - 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.ProxyHa ndler({"http" : \
"http://%(user)s:%(pass )s@%(host)s:%(p ort)d" % proxy_info})
opener = urllib2.build_o pener(proxy_sup port, urllib2.HTTPHan dler)

# 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
21118
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?
1
2939
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 must be screwing up my request when I try to send it. Previously the code worked fine, but the problem occured when I included the proxy/authentication code, so that must be what is messing around with my request.
0
2149
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 our Internet proxy server (I was trying to contact an internal machine). Upon digging into the code a little, I found that urllib2 was pulling the proxy information out of the Windows registry (Internet Explorer uses these settings). However,...
1
3372
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 the Authorization headers being sent for the request.
2
7763
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
8520
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 make the NTLM authentication, but you have to run it and make all your connection through it, not an optimal solution. So what I really need is an enhanced urllib2 that support NTLM or Kerberos. I've found that pywin32 could manage NTLM encryption...
1
5760
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
6
3476
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. However, this does not set the proxy: httpproxy = '127.0.0.1:3129' proxy_support = urllib2.ProxyHandler({"http":"http://" + httpproxy}) opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler) urllib2.install_opener(opener)
6
4530
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 authentication to connect TO an external Web Server, but not how to go THROUGH a (corporate) Proxy, thus my question here. I've tried all sorts of permutations based on my findings, but (obviously) nothing's working for me. I've come across conflicting
0
9511
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
10404
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...
0
10195
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
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
9016
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...
0
6765
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
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...
1
4090
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
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.