【问题标题】:How to connect to a HTTPS proxy (secure web proxy) in pycurl?如何在 pycurl 中连接到 HTTPS 代理(安全 Web 代理)?
【发布时间】:2020-04-18 03:12:33
【问题描述】:

我正在尝试使用 pycurl 连接到 secure web proxy。在尝试设置适当的代理类型时,这些选项可用,它们对应于 curl 代理选项(在括号中):

 - "PROXYTYPE_HTTP" (CURLPROXY_HTTP)
 - "PROXYTYPE_HTTP_1_0" (CURLPROXY_HTTP_1_0)
 - "PROXYTYPE_SOCKS4" (CURLPROXY_SOCKS4)
 - "PROXYTYPE_SOCKS4A" (CURLPROXY_SOCKS4A)
 - "PROXYTYPE_SOCKS5" (CURLPROXY_SOCKS5)
 - "PROXYTYPE_SOCKS5_HOSTNAME" (CURLPROXY_SOCKS5_HOSTNAME)

但是,还有一个名为 CURLPROXY_HTTPS 的 curl 选项,如 docs 中所述,似乎不可用。

使用普通 curl,我使用此命令连接到代理:

curl --proxy https://proxy-host:proxy-port --proxy-insecure -U username:password https://target.com

一切都按预期工作。但不是 pycurl。

如何在 pycurl 中实现相同的行为?

【问题讨论】:

    标签: curl proxy pycurl


    【解决方案1】:

    suggestion 之后,我在 pycurl github 问题中找到了 CURLPROXY_HTTPS 的选项代码,它是 2

    我能够使用以下代码通过带有 pycurl 的安全 Web 代理发出请求:

    import pycurl
    from io import BytesIO
    import certifi
    
    
    def request_with_pycurl(username, password, host, port, target_url='https://api.ipify.org/'):
        buffer = BytesIO()
        c = pycurl.Curl()
    
        c.setopt(pycurl.CAINFO, certifi.where())
    
        # set proxy-insecure
        c.setopt(c.PROXY_SSL_VERIFYHOST, 0)
        c.setopt(c.PROXY_SSL_VERIFYPEER, 0)
    
        # set headers
        c.setopt(pycurl.USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0')
    
        # set proxy
        c.setopt(pycurl.PROXY, f"https://{host}:{port}")
    
        # proxy auth
        c.setopt(pycurl.PROXYUSERPWD, f"{username}:{password}")
    
        # set proxy type = "HTTPS"
        c.setopt(pycurl.PROXYTYPE, 2)
    
        # target url
        c.setopt(c.URL, target_url)
    
        c.setopt(c.WRITEDATA, buffer)
        c.perform()
        c.close()
    
        body = buffer.getvalue()
        return body
    
    
    response = request_with_pycurl("proxy_username", "proxy_password", "proxy_host", "proxy_port").decode()
    
    print(response)
    

    【讨论】:

      【解决方案2】:

      如果以上答案不起作用,其他人来为 crawlera 代理或无密码代理,这里是 Andriy Stolyar answer 的更新,

      def request_with_pycurl(username, password, host, port, target_url='http://api.ipify.org/'):
          buffer = BytesIO()
          c = pycurl.Curl()
      
          c.setopt(pycurl.CAINFO, certifi.where())
      
          # set proxy-insecure
          c.setopt(c.PROXY_SSL_VERIFYHOST, 0)
          c.setopt(c.PROXY_SSL_VERIFYPEER, 0)
      
          # set headers
          c.setopt(pycurl.USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0')
      
          # set proxy
          c.setopt(pycurl.PROXY, f"http://{host}:{port}")
      
          # proxy auth
          c.setopt(pycurl.PROXYUSERNAME, username)
      
          # set proxy type = "HTTPS"
          #c.setopt(pycurl.PROXYTYPE, 2)
      
          # target url
          c.setopt(c.URL, target_url)
      
          c.setopt(c.WRITEDATA, buffer)
          c.perform()
          c.close()
      
          body = buffer.getvalue()
          return body
      
      
      response = request_with_pycurl("KEY:", "", "HOST", "PORT").decode()
      
      print(response)
      

      【讨论】:

        猜你喜欢
        • 2015-12-06
        • 1970-01-01
        • 1970-01-01
        • 2016-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多