Proxies with Python 'Requests' module

后端 未结 10 892
傲寒
傲寒 2020-11-22 12:13

Just a short, simple one about the excellent Requests module for Python.

I can\'t seem to find in the documentation what the variable \'proxies\' should contain. Whe

10条回答
  •  Happy的楠姐
    2020-11-22 12:34

    The proxies' dict syntax is {"protocol":"ip:port", ...}. With it you can specify different (or the same) proxie(s) for requests using http, https, and ftp protocols:

    http_proxy  = "http://10.10.1.10:3128"
    https_proxy = "https://10.10.1.11:1080"
    ftp_proxy   = "ftp://10.10.1.10:3128"
    
    proxyDict = { 
                  "http"  : http_proxy, 
                  "https" : https_proxy, 
                  "ftp"   : ftp_proxy
                }
    
    r = requests.get(url, headers=headers, proxies=proxyDict)
    

    Deduced from the requests documentation:

    Parameters:
    method – method for the new Request object.
    url – URL for the new Request object.
    ...
    proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
    ...


    On linux you can also do this via the HTTP_PROXY, HTTPS_PROXY, and FTP_PROXY environment variables:

    export HTTP_PROXY=10.10.1.10:3128
    export HTTPS_PROXY=10.10.1.11:1080
    export FTP_PROXY=10.10.1.10:3128
    

    On 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
    

    Thanks, Jay for pointing this out:
    The syntax changed with requests 2.0.0.
    You'll need to add a schema to the url: https://2.python-requests.org/en/latest/user/advanced/#proxies

提交回复
热议问题