HTTPS代理不适用于Python的请求模块 [英] HTTPS proxies not working with Python's requests module

查看:188
本文介绍了HTTPS代理不适用于Python的请求模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python还是很陌生,我一直在使用他们的请求模块代替PHP的cURL库.我的代码如下

I'm pretty new to Python and I've been using their requests module as a substitute for PHP's cURL library. My code is as follows

import requests
import json
import os
import urllib
import math
import sys

def main() :    
   url = 'https://api.com'

   headers = {'Content-Type': 'application/json; charset=utf-8',
              'User-Agent': '(iPhone; iOS 7.0.4; Scale/2.00)'}

   d = {'token': "12345"}

   proxies = {
      "https": "https://27.254.52.99:8080",
   }

   post = json.dumps(d);
   r = requests.post(url, data=post, headers=headers, proxies=proxies)
   print r.json

if __name__ == "__main__":
    main()

但是,我遇到以下错误:

However, I'm greeted with the following error:

File "test.py", line 42, in test
r = requests.post(url, data=post, headers=headers, proxies=proxies)
File "/Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg/requests/api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "/Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg/requests/sessions.py", line 383, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg/requests/sessions.py", line 486, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests-2.2.1-py2.7.egg/requests/adapters.py", line 381, in send
raise ProxyError(e)
ProxyError: Cannot connect to proxy. Socket error: [Errno 54] Connection reset by peer.

推荐答案

编辑2019年6月:此答复不再相关.问题已解决.

编辑2 :请注意,即使对于https代理,代理地址的方案也是http,这是因为客户端和代理服务器会以纯http形式启动隧道(CONNECT方法).但是, 3年前可能并非如此." -从评论中

Edit 2: "note that even for https proxy, the proxy address' scheme is http, it's because the client and proxy server initiate the tunnelling(the CONNECT method) in plain http. However, that's may not be true 3 years ago." - From the comments

HTTPS在请求中被调试".我不知道具体细节,但您可以在本网站上找到有关此问题的其他一些主题.同样,在此处,仍然存在Github问题.我怀疑您遇到那里提到的问题.如果我完全错了,请有人纠正我.

HTTPS is 'bugged' in requests. I don't know the specifics but you can find a few other topics on this website concerning the issue. Also a Github issue is still active here. I'm suspecting you're having the problems mentioned there. If I am totally wrong, someone correct me.

要验证:

$~ curl --proxy https://27.254.52.99:8080 icanhazip.com
27.254.52.99

有效,但随后在Python中运行:

Works, but then in Python:

>>> proxies={'https': 'https://27.254.52.99:8080'}
>>> r = requests.get('http://icanhazip.com', headers={'User-Agent': 'Bla'}, proxies=proxies)
print r.content
<my ipv6 address comes up>

如您所见,我的地址出现了,这意味着代理什么也没做.

As you can see, my address comes up which means the proxy did nothing.

我不明白您为什么收到堆栈跟踪信息.也许是因为您的API也在HTTPS上(?).也许您的API只是...下降了.

I don't understand why you are receiving a stacktrace. Maybe because your API is on HTTPS as well (?). Or maybe your API is just... down.

无论如何,如果代理通过HTTP,则代理确实可以处理请求.

Anyway, the proxy does work in requests if its over HTTP.

>>> proxies={'http': 'http://27.254.52.99:8080'}
>>> r = requests.head('http://icanhazip.com', headers={'User-Agent': 'Bla'}, proxies=proxies)
print r.content
27.254.52.99

这篇关于HTTPS代理不适用于Python的请求模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆