我有以下代码:
def connect(module, action, input={}):
data = {'module': module, 'action': action, 'input': json.dumps(input),
'token': token, 'request_id': 1}
headers = {'Cookie': 'TNS_SESSIONID=' + cookie}
url = server + '/request.php'
try:
request = urllib.request.Request(url, urllib.parse.urlencode(data), headers)
context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
response = urllib.request.urlopen(request, context)
content = json.loads(response.read())
return content['response']
except Exception as e:
print("Error: " + str(e))
return None
但我一直收到错误:"错误:'模块'对象没有属性' PROTOCOL_TLSv1_2'"
如果我尝试使用TLS 2或SSL2或3,我会遇到错误"错误:" 已经记录了,我相信常见的解决方法是使用TLSv1(已禁用)
我有什么办法可以让它工作,还是我必须等待管理员权限并下载并安装python 2.7.9+或3.4+? (我目前正在使用Portablepython,因此限制了版本号)
答案 0 :(得分:1)
来自https://docs.python.org/3/library/ssl.html:
ssl.PROTOCOL_TLSv1_2
...Available only with openssl version 1.0.1+.
New in version 3.4.
因此您需要升级才能获得此选项。
您可以尝试PROTOCOL_SSLv23
,它提供客户端可以以向后兼容的方式执行的最佳协议版本。但是许多配置为仅允许TLS 1.2的服务器仍然会导致使用此选项的握手失败。
除此之外,你需要OpenSSL 1.0.1确实支持TLS1.2。