how to set proxy in python requests

How to Set Proxy in Python Requests

If you want to access a website through a proxy using Python Requests library, then you can do it by setting the proxy in the code. There are a few ways you can set a proxy in Python Requests.

Using HTTP Proxy

If you want to set up an HTTP proxy to access the website through the Requests library, then you can use the following code:


import requests

proxy = {
  "http": "http://proxy_address:proxy_port",
}

response = requests.get("http://example.com", proxies=proxy)

print(response.content)

In the above code, replace proxy_address and proxy_port with the address and port of your HTTP proxy.

Using HTTPS Proxy

If you want to set up an HTTPS proxy to access the website through the Requests library, then you can use the following code:


import requests

proxy = {
  "https": "https://proxy_address:proxy_port",
}

response = requests.get("https://example.com", proxies=proxy)

print(response.content)

In the above code, replace proxy_address and proxy_port with the address and port of your HTTPS proxy.

Using SOCKS Proxy

If you want to set up a SOCKS proxy to access the website through the Requests library, then you can use the following code:


import requests

proxy = {
  "http": "socks5://proxy_address:proxy_port",
  "https": "socks5://proxy_address:proxy_port",
}

response = requests.get("https://example.com", proxies=proxy)

print(response.content)

In the above code, replace proxy_address and proxy_port with the address and port of your SOCKS proxy.