Addictive point when going through http proxy with basic authentication in python

Addictive point when going through http proxy with basic authentication in python

2.7.x series. When basic authentication is applied when going through http proxy with urllib2.

When the target server is authenticated

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.

When basic authentication is applied to the proxy server

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.

By the way

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

Addictive point when going through http proxy with basic authentication in python
Send HTTP with Basic authentication header in Python
A addictive point in "Bayesian inference experience with Python"
BASIC authentication with Python bottle
Basic authentication with an encrypted password (.htpasswd) in bottle with python
Scraping with Selenium in Python (Basic)
Achieve Basic Authentication with CloudFront Lambda @ Edge with Python 3.8
Precautions when dealing with control structures in Python 2.6
Pass the authentication proxy through communication using python urllib3
Mailbox selection when retrieving Gmail with imaplib in python
Japanese output when dealing with python in visual studio
Basic sorting in Python
Http request in python
HTTP communication with Python
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
Show decimal point in Python
Scraping with selenium in Python
Refactoring Learned in Python (Basic)
Working with LibreOffice in Python
Debugging with pdb in Python
Use HTTP cache in Python
[Python] Using OpenCV with Python (Basic)
Working with sounds in Python
Scraping with Tor in Python
Tweet with image in Python
Attention when os.mkdir in Python
Combined with permutations in Python
Error when playing with python
Easy HTTP server with Python
[Python] [SQLite3] Operate SQLite with Python (Basic)
How to not escape Japanese when dealing with json in python
Firebase Authentication token issuance in Python and token verification with Fast API