urllib2 request htaccess page through proxy

A

Alessandro Fachin

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
proxy_handler = urllib2.ProxyHandler({'http': 'http://myproxy:8888'})

#htaccess set up
user="matteo"
password="matteo"
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)

opener = urllib2.build_opener(proxy_handler,authhandler)
urllib2.install_opener(opener)

#open the url
req=urllib2.Request(url)
data=urllib2.urlopen(req).read()
print data

i get no access on access.log from apache2 and nothing from the proxy in
tcpdump log. If i use only the proxy with a page that doesn't use htaccess
it works... if anyone could help,regards...




Traceback (most recent call last):
File "proxy.py", line 22, in ?
data=urllib2.urlopen(req).read()

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 130, in urlopen
return _opener.open(url, data)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 364, in open
response = meth(req, response)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 471, in http_response
response = self.parent.error(

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 396, in error
result = self._call_chain(*args)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 337, in _call_chain
result = func(*args)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 741, in http_error_401
host, req, headers)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 720, in http_error_auth_reqed
return self.retry_http_basic_auth(host, req, realm)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 730, in retry_http_basic_auth
return self.parent.open(req)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 364, in open
response = meth(req, response)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 471, in http_response
response = self.parent.error(

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 402, in error
return self._call_chain(*args)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 337, in _call_chain
result = func(*args)

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/urllib2.py",
line 480, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Authorization Required
 
J

John J. Lee

Alessandro Fachin said:
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... [...]
i get no access on access.log from apache2 and nothing from the proxy in
tcpdump log. If i use only the proxy with a page that doesn't use htaccess
it works... if anyone could help,regards... [...]
urllib2.HTTPError: HTTP Error 401: Authorization Required

Works for me (using Squid and Apache both running on localhost).
Looking at what goes over the wire, I see a 401 followed by a 299
response, which is what is expected (the 401 prompts the client to
send the username and password, which is then rewarded with the 200).

Could you describe in more detail what your proxy and Apache
configuration are like? And more about your network (on what machines
do proxy, HTTP server, and HTTP client run -- all on the same machine,
some on the same machine, all three on three different machines?) The
more detail the better.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,708
Messages
2,569,347
Members
44,653
Latest member
Angus30T19

Latest Threads

Top