在python2.7.10pycon中全局禁用ssl检查

2024-05-15 06:30:35 发布

您现在位置:Python中文网/ 问答频道 /正文

在我不知道如何传递ssl上下文的情况下,请建议一种全局禁用证书验证检查的方法。在

import pycontrol.pycontrol as pc

b = pc.BIGIP(
hostname = "xx.xx.xx.xx",
username = "xxxxxxx",
password = "xxxxxxx",
fromurl = True,
wsdls = ['GlobalLB.WideIP','GlobalLB.Pool']
)

<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>

如果版本低于2.7.6,它可以正常工作,但我想知道如何使它在2.7.10中工作。在


Tags: 方法importsslas情况全局建议hostname
2条回答

我在2.7.9中遇到过这个问题,并且在我只针对内部网站点编写脚本的情况下(我有0的机会安装完整的证书),我采取了使用2.7.8的简单方法。在

我在2.7.10通过使用一个定制的suds客户端修复了这个问题:

import urllib2
import ssl
from suds import transport
from suds.client import Client
from suds.xsd.doctor import Import, ImportDoctor
from pycontrol import pycontrol

IMP = Import('http://schemas.xmlsoap.org/soap/encoding/')
DOCTOR = ImportDoctor(IMP)
ICONTROL_URI = '/iControl/iControlPortal.cgi'
SESSION_WSDL = 'System.Session'

class HTTPSUnVerifiedCertTransport(transport.https.HttpAuthenticated):
    def __init__(self, *args, **kwargs):
        transport.https.HttpAuthenticated.__init__(self, *args, **kwargs)
    def u2handlers(self):
        handlers = []
        handlers.append(urllib2.ProxyHandler(self.proxy))
        handlers.append(urllib2.HTTPBasicAuthHandler(self.pm))
        # python ssl Context support - PEP 0466
        if hasattr(ssl, '_create_unverified_context'):
            ssl_context = ssl._create_unverified_context()
            handlers.append(urllib2.HTTPSHandler(context=ssl_context))
        else:
            handlers.append(urllib2.HTTPSHandler())
        return handlers

def new_get_suds_client(self, url, **kw):
        if not url.startswith('https'):
            t = transport.http.HttpAuthenticated(username=self.username,
                                                 password=self.password)
            c = Client(url, transport=t, username=self.username,
                         password=self.password, doctor=DOCTOR, **kw)
        else:
            t = HTTPSUnVerifiedCertTransport(username=self.username,
                                             password=self.password)
            c = Client(url, transport=t, username=self.username,
                         password=self.password, doctor=DOCTOR, **kw)
        return c

pycontrol.BIGIP._get_suds_client = new_get_suds_client
b = pycontrol.BIGIP(
    hostname = "xx.xx.xx.xx",
    username = "xxxxxxx",
    password = "xxxxxxx",
    fromurl = True,
    wsdls = ['GlobalLB.WideIP','GlobalLB.Pool']
    )

相关问题 更多 >

    热门问题