[英]Disable SSL certificate check Twisted Agents
我正在使用 Twisted (16.3) 和 Treq (15.1) 在 Python (2.7) 中發出異步請求。
我在處理某些通過 HTTPS 的請求時遇到問題。
某些網站的證書無效,因此在向他們提出請求時,我得到以下信息:
twisted.python.failure.Failure OpenSSL.SSL.Error
我希望我的客戶信任任何服務器,包括那些沒有證書或有自簽名證書的服務器。
如何在我的客戶端上禁用證書檢查?
這是一個與我基本相同的問題: https : //stackoverflow.com/questions/34357439/ssl-options-for-twisted-agents
謝謝!
這是為treq
制作域白名單的簡單方法
from treq.client import HTTPClient
from twisted.web.iweb import IPolicyForHTTPS
from twisted.web.client import BrowserLikePolicyForHTTPS, Agent
from twisted.internet.ssl import CertificateOptions
from twisted.internet import task, defer, ssl
from zope.interface import implementer
@implementer(IPolicyForHTTPS)
class WhitelistContextFactory(object):
def __init__(self, good_domains=None):
"""
:param good_domains: List of domains. The URLs must be in bytes
"""
if not good_domains:
self.good_domains = []
else:
self.good_domains = good_domains
# by default, handle requests like a browser would
self.default_policy = BrowserLikePolicyForHTTPS()
def creatorForNetloc(self, hostname, port):
# check if the hostname is in the the whitelist, otherwise return the default policy
if hostname in self.good_domains:
return ssl.CertificateOptions(verify=False)
return self.default_policy.creatorForNetloc(hostname, port)
@task.react
@defer.inlineCallbacks
def main(reactor):
# make a custom client, agent, and context factory
# NOTE: WhitelistContextFactory() takes a list of BYTES
treq = HTTPClient(Agent(reactor, contextFactory=WhitelistContextFactory([b'example.net'])))
response = yield treq.get('https://example.net/version')
content = yield response.content()
print(content)
WhitelistContextFactory
獲取 URL list
(以bytes
)並檢查hostname
是否在列表中以忽略 TLS 驗證。 您也可以花哨並使用正則表達式。 感謝https://github.com/twisted/treq/issues/213
在過去的幾天里,我也一直在嘗試這樣做。 通過我為規避證書驗證所做的所有努力,我可以輕松地創建一對密鑰並繼續我的快樂:D。 我在treq
問題的treq
問題板上發現了這個評論:
from twisted.internet import _sslverify _sslverify.platformTrust = lambda : None
我確信有一種“正確”的方法可以做到這一點,但在我看來這不值得付出努力。 我做了一個補丁,這樣它就不會覆蓋 platformTrust()
,我會嘗試合並它,但我不會屏住呼吸。從我看到的一些關於信任根、ssl 和證書的錯誤評論的語氣來看,我認為它們不會被合並。 希望這有幫助。
如果您想完全忽略 SSL 檢查,這里有一個解決方法:
from twisted.internet import ssl, _sslverify
from twisted.web.iweb import IPolicyForHTTPS
@implementer(IPolicyForHTTPS)
class IgnoreHTTPS:
def creatorForNetloc(self, hostname, port):
options = ssl.CertificateOptions(verify=False)
return _sslverify.ClientTLSOptions(hostname.decode('ascii'), options.getContext())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.