我正试着用pycurl连接secure web proxy。尝试设置适当的代理类型时,可以使用以下选项,这些选项对应于卷曲代理选项(在括号中):
- "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,我使用以下命令连接到代理:
curl --proxy https://proxy-host:proxy-port --proxy-insecure -U username:password https://target.com一切都像预期的那样工作。但不是用pycurl。
如何在pycurl中实现相同的行为?
发布于 2020-04-20 05:24:31
根据我在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)发布于 2020-09-07 15:37:44
如果上面的答案不起作用,其他任何人都会来找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)https://stackoverflow.com/questions/61283802
复制相似问题