【问题标题】:How to tell if a GET request in Python is using an https or http proxy?如何判断 Python 中的 GET 请求是使用 https 还是 http 代理?
【发布时间】:2018-09-05 06:28:23
【问题描述】:

我目前在通过 Python3 中的 requests 库发出 get 请求时使用代理。我为我的代理设置了 Tor 和 Privoxy,代码如下所示:

import requests
proxies = {
    "http": "http://127.0.0.1:8118",
    "https": "https://127.0.0.1:8118"
}
resp = requests.get("https://icanhazip.com", proxies=proxies)

我想知道如果使用了 http 或 https 代理,是否有办法查看 resp。有没有办法做到这一点?

【问题讨论】:

    标签: proxy python-requests tor


    【解决方案1】:

    我认为最pythonic的方式是这样的:

    import requests
    from urllib.parse import urlparse
    
    def get_scheme(url):
        return urlparse(url).scheme
    
    url = 'https://www.whatever.com'
    response = requests.get(url)
    
    scheme = get_scheme(response.url)
    

    或者,您可以这样做:

    import requests
    
    url = 'https://www.whatever.com'
    
    response = requests.get(url)
    
    scheme = response.url.split(':')[0]
    

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 2019-02-13
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 2013-01-15
      • 2012-05-08
      • 1970-01-01
      相关资源
      最近更新 更多