Last modified: Jul 02, 2023 By Alexander Williams
Use Python Requests with Proxy (Username and Password)
In this guide, you’ll see how to use a proxy server with authentication (username and password) when making requests using the requests
Here's an example:
import requests
# Proxy server details
proxy_url = 'http://proxy.example.com:8080'
proxy_username = 'your-username'
proxy_password = 'your-password'
# Construct the proxy dictionary with authentication details
proxies = {
'http': f'http://{proxy_username}:{proxy_password}@{proxy_url}',
'https': f'http://{proxy_username}:{proxy_password}@{proxy_url}'
}
# The URL of the website to access through the proxy
url = 'https://example.com'
# Send a GET request using the specified proxy
response = requests.get(url, proxies=proxies)
- The
proxy_urlURL and port of the proxy server. - The
proxy_usernameandproxy_passwordusername and password for proxy authentication. - The
proxiesdictionary is constructed with the necessary keys and values to include the proxy details and authentication information. - The
urlURL of the website to access through the proxy. - The
requests.get()function sends a GET request to the specified URL.