如何在 pycurl 中连接到 HTTPS 代理(安全 Web 代理)?
How to connect to a HTTPS proxy (secure web proxy) in 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)
但是,还有一个名为 CURLPROXY_HTTPS
的 curl 选项,如 docs 中所述,它似乎不可用。
使用普通 curl,我使用此命令连接到代理:
curl --proxy https://proxy-host:proxy-port --proxy-insecure -U username:password https://target.com
一切都按预期进行。但 pycurl 不行。
如何在 pycurl 中实现相同的行为?
在 suggestion 我进入 pycurl github 问题之后,我找到了 CURLPROXY_HTTPS 的选项代码,它是 2
。
我能够使用下一个代码通过安全的网络代理和 pycurl 发出请求:
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)
如果上面的答案不起作用,还有其他人来找 crawlera 代理或没有密码的代理,这里是 、
的更新
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)
我正在尝试使用 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 中实现相同的行为?
在 suggestion 我进入 pycurl github 问题之后,我找到了 CURLPROXY_HTTPS 的选项代码,它是 2
。
我能够使用下一个代码通过安全的网络代理和 pycurl 发出请求:
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)
如果上面的答案不起作用,还有其他人来找 crawlera 代理或没有密码的代理,这里是
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)