Python Requests Library Use Proxy
If you want to make a request using Python Requests library through a proxy server, you can do it with the help of the proxies
parameter. In this parameter, you need to pass a dictionary containing one or more entries where each entry represents a proxy server. A sample code for this is given below.
import requests
proxies = {
"http": "http://proxy.example.com:8080",
"https": "https://proxy.example.com:8080"
}
response = requests.get("http://www.example.com", proxies=proxies)
print(response.status_code)
In the above code, we have defined a dictionary proxies
which contains two entries - one for HTTP and another for HTTPS. The value for each entry is the URL of the proxy server which includes the protocol (http or https) and the port number (usually 8080). After that, we have made a GET request to http://www.example.com
and passed the proxies
parameter to include the proxy information. Finally, we have printed the status code of the response.
Multiple Proxies
If you want to use multiple proxies for different protocols, you can include them in the proxies
dictionary as shown below.
import requests
proxies = {
"http": "http://proxy1.example.com:8080",
"https": "https://proxy2.example.com:8080",
"ftp": "ftp://proxy3.example.com:8080"
}
response = requests.get("http://www.example.com", proxies=proxies)
print(response.status_code)
In the above code, we have defined three entries in the proxies
dictionary for HTTP, HTTPS, and FTP protocols respectively. Each entry contains the URL of a different proxy server. You can include as many entries as you want for different protocols and proxy servers.