【发布时间】:2015-04-30 12:16:38
【问题描述】:
在我的扭曲应用程序中,我想向 Akismet 发出异步请求以检查垃圾邮件。 Akismet 合理地使用 HTTPS,所以我一直在关注文档中的 web client guide on SSL。但是有这部分让我担心:
这是一个示例,展示了如何使用代理请求一个没有证书验证的 HTTPS URL。
我非常希望通过证书验证来防止中间人攻击。那么如何添加呢?
我的未经验证的测试代码是这样的:
from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.internet.ssl import ClientContextFactory
class WebClientContextFactory(ClientContextFactory):
def getContext(self, hostname, port):
print( "getting context for {}:{}".format( hostname, port ) )
# FIXME: no attempt to verify certificates!
return ClientContextFactory.getContext(self)
agent = Agent( reactor, WebClientContextFactory() )
def success( response ):
print( "connected!" )
def failure( failure ):
print( "failure: {}".format( failure ) )
def stop( ignored ):
reactor.stop()
agent.request( "GET", "https://www.pcwebshop.co.uk/" )\ # uses self-signed cert
.addCallbacks( success, failure )\
.addBoth( stop )
reactor.run()
由于无法验证证书,我希望它失败。
【问题讨论】:
标签: python ssl https twisted twisted.web