How can I set a single proxy for a requests session object?

后端 未结 4 1983
走了就别回头了
走了就别回头了 2020-12-15 06:42

I\'m using the Python requests package to send http requests. I want to add a single proxy to the requests session object. eg.

session = requests.Session()
s         


        
4条回答
  •  孤城傲影
    2020-12-15 07:14

    In addition to @neowu' answer, if you would like to set a proxy for the lifetime of a session object, you can also do the following -

    import requests
    proxies = {'http': 'http://10.11.4.254:3128'}
    s = requests.session()
    s.proxies.update(proxies)
    s.get("http://www.example.com")   # Here the proxies will also be automatically used because we have attached those to the session object, so no need to pass separately in each call
    

提交回复
热议问题