Learner
Learner

Reputation: 11

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

Upvotes: 0

Views: 1856

Answers (2)

flaxon
flaxon

Reputation: 942

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

Upvotes: 1

PicxyB
PicxyB

Reputation: 847

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

Upvotes: 0

Related Questions