2.7.x series. When basic authentication is applied when going through http proxy with urllib2.
client => proxy => target
↑ Here
This is okay if you imitate the reference or the one that comes out when you google normally.
#!/usr/bin/env python
import urllib2
proxy = urllib2.ProxyHandler({
'http': 'http://username:password@proxy:8080'
})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth)
urllib2.install_opener(opener)
conn = urllib2.urlopen('http://target/')
print conn.read()
This will give you the Proxy-Authorization
header.
client => proxy => target
↑ Here
In this case, you must add the ʻAuthorization header instead of the
Proxy-Authorization` header.
However, no matter how much I searched and tried, the method using Handler did not come out, so
It can't be helped, so I managed to solve it by calculating the header by hand and adding it.
#!/usr/bin/env python
import urllib2
import base64
proxy = urllib2.ProxyHandler({
'http':'http://proxy:8080'
})
opener = urllib2.build_opener(proxy)
base64string = base64.encodestring('%s:%s' % ("username", "password"))
opener.addheaders = [ ("Authorization", "Basic %s" % base64string) ]
conn = opener.open('http://target/')
print conn.read()
I wonder if there is a proper solution using Handler.
pip requests
When used
It was the behavior of Proxy-Authorization
.
#!/usr/bin/env python
import requests
proxies = {
"http": "http://username:password@proxy:8080",
}
ret = requests.get("http://target/", proxies=proxies)
print ret
Recommended Posts