may*_*k93 3 python ssl twisted twisted.web twisted.internet
我使用Twisted(16.3)和Treq(15.1)在Python(2.7)中发出异步请求.
我遇到了一些基于HTTPS的请求问题.
有些网站的证书无效,因此在向他们提出请求时,我得到了这个:
twisted.python.failure.Failure OpenSSL.SSL.Error
Run Code Online (Sandbox Code Playgroud)
我希望我的客户端信任任何服务器,包括那些没有证书或自签名证书的服务器.
如何在我的客户端上禁用证书检查?
这是一个与我的问题基本相同的问题: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)
Run Code Online (Sandbox Code Playgroud)
WhitelistContextFactory获取一个listURL(in bytes)并检查hostname列表中是否有忽略TLS验证.你也可以使用正则表达式.感谢https://github.com/twisted/treq/issues/213
在过去的几天里,我也一直在努力做到这一点.通过我为避免证书验证所做的所有努力,我可以很容易地创建一对密钥并且一直在我的快乐方式:D.我在发布问题的问题板上发现了这条评论treq:
from twisted.internet import _sslverify
_sslverify.platformTrust = lambda : None
Run Code Online (Sandbox Code Playgroud)
我确信有一种令人费解的方式"正确"地做到这一点,但在我看来这不值得.我做了一个补丁,它不会覆盖platformTrust(),我会尝试让它合并,但我不会屏住呼吸.从我见过的关于信任根,ssl和证书的一些bug评论的基调来看,我认为它不会被合并.希望这会有所帮助.
| 归档时间: |
|
| 查看次数: |
1702 次 |
| 最近记录: |