0

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

Learner
  • 11
  • 1

2 Answers2

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#id@company.com'
password = 'dddd^ff'
proxy = 'http://'+urllib.parse.quote(username)+':'+urllib.parse.quote(password)+'@foo.com/path/'


will return

http://dav%23id%40company.com:dddd%5Eff@foo.com:8080
flaxon
  • 928
  • 4
  • 18
0

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

PicxyB
  • 596
  • 2
  • 8
  • 27