'What is the correct way of passing proxy user and proxy password in python requests?
I am using proxy in python requests and have followig question. The syntax
http://username:password@host:port
Does the usename and passowrd have to be encoded like quote(usrname) and quote(password) as the username and passowrd can have special chars like #, $ etc.
Does requests need encoding ?
Any github link would be helpful
Solution 1:[1]
you need to replace the special chars with HTML entities if you encounter a problem according to here https://stackoverflow.com/a/6718568/12239849
import urllib.parse
username = 'dav#[email protected]'
password = 'dddd^ff'
proxy = 'http://'+urllib.parse.quote(username)+':'+urllib.parse.quote(password)+'@foo.com/path/'
will return
http://dav%23id%40company.com:dddd%[email protected]:8080
Solution 2:[2]
You just need to setup you proxy like:
# Import
import requests
# Proxy
proxies = {
'http': 'http://username:password@host:port',
'https': 'http://username:password@host:port'
}
# Main thread
if __name__ == "main":
r = requests.get("www.google.com", proxies=proxies)
This works with utf-8 encoding
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | PicxyB |
