如何在pycurl中连接到HTTPS代理(安全Web代理)?

时间:2020-04-18 03:12:33

标签: curl proxy pycurl

我正在尝试使用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)

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

使用普通卷曲,我使用以下命令连接到代理:

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

一切都按预期进行。但是不使用pycurl。

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

2 个答案:

答案 0 :(得分:1)

在遇到pycurl github问题的suggestion之后,我找到了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)

答案 1 :(得分:0)

如果上述答案不起作用,其他人都会来找爬虫代理或没有密码的代理,我已经更新了https://stackoverflow.com/users/5952509/andriy-stolyar答案,

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)