Home > Backend Development > Python Tutorial > How to Properly Configure Proxies with Python\'s `requests` Module?

How to Properly Configure Proxies with Python\'s `requests` Module?

Barbara Streisand
Release: 2024-11-28 10:14:11
Original
293 people have browsed it

How to Properly Configure Proxies with Python's `requests` Module?

Proxies with Python 'Requests' Module

Understanding the 'proxies' Parameter

When configuring a 'Requests' request using the 'proxies' parameter, it's essential to understand the expected format of the value. Contrary to immediate assumptions, it's not sufficient to provide an "IP:PORT" string.

Proper Syntax of 'proxies'

Instead, the 'proxies' parameter expects a dictionary in the following format:

{
  "protocol1": "scheme1://ip1:port1",
  "protocol2": "scheme2://ip2:port2",
  ...
}
Copy after login

Example Usage

Consider the following example:

http_proxy = "http://10.10.1.10:3128"
https_proxy = "https://10.10.1.11:1080"
ftp_proxy = "ftp://10.10.1.10:3128"

proxies = { 
              "http": http_proxy, 
              "https": https_proxy, 
              "ftp": ftp_proxy
            }

r = requests.get(url, headers=headers, proxies=proxies)
Copy after login

In this example:

  • Different proxies are specified for the "http", "https", and "ftp" protocols.
  • The 'Requests' library will automatically use the appropriate proxy based on the URL's protocol.

Environment Variables

Alternatively, instead of using the 'proxies' parameter, you can set environment variables to configure proxies on Linux and Windows:

  • Linux:

    export HTTP_PROXY=10.10.1.10:3128
    export HTTPS_PROXY=10.10.1.11:1080
    export FTP_PROXY=10.10.1.10:3128
    Copy after login
  • Windows:

    set http_proxy=10.10.1.10:3128
    set https_proxy=10.10.1.11:1080
    set ftp_proxy=10.10.1.10:3128
    Copy after login

The above is the detailed content of How to Properly Configure Proxies with Python\'s `requests` Module?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template