首先,我知道 TLSv1.0 非常旧,不应该使用,但我需要连接到一些不支持任何其他 atm 的非常旧的本地硬件
#import ssl
from OpenSSL import SSL
try:
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
except ImportError:
pass
import requests sys, os, select, socket
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
from requests.packages.urllib3.util import ssl_
from requests.packages.urllib3.contrib import py
CIPHERS = (
'ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:
ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:AES256-SHA:'
)
class TlsAdapter(HTTPAdapter):
def __init__(self, ssl_options=0, **kwargs):
self.ssl_options = ssl_options
super(TlsAdapter, self).__init__(**kwargs)
def init_poolmanager(self, *pool_args, **pool_kwargs):
ctx = SSL.Context(SSL.TLSv1_METHOD)
self.poolmanager = PoolManager(*pool_args,
ssl_context=ctx,
**pool_kwargs)
session = requests.Session()
adapter = TlsAdapter(ssl.OP_NO_TLSv1_1 | ssl.OP_NO_TLSv1_2)
session.mount("https://", adapter)
data = { "key":"value"}
try:
r = session.post("https://192.168.1.1", data)
print(r)
except Exception as exception:
print(exception)
我尝试了几种方法。上面的代码大部分是从过去发布的类似问题中提取的,但是 python3 ssl 不再支持 TLSv1,因此它会引发不受支持的协议错误。我添加了“import urllib3.contrib.pyopenssl”来尝试强制它使用 pyOpenSSL,而不是按照这个 urllib3 文档。此代码的当前错误是
load_verify_locations() takes from 2 to 3 positional arguments but 4 were given
我知道这是来自 urllib3 上下文的验证部分,我需要修复 pyOpenSSL 的上下文,但我一直被困在这里试图修复上下文。