Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
looyolo
scrapy
提交
30a27eff
S
scrapy
项目概览
looyolo
/
scrapy
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
scrapy
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
30a27eff
编写于
2月 19, 2016
作者:
P
Paul Tremberth
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use best practices for TLS connections when using Twisted>=14.0
上级
da36b7d3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
70 addition
and
26 deletion
+70
-26
scrapy/core/downloader/contextfactory.py
scrapy/core/downloader/contextfactory.py
+70
-26
未找到文件。
scrapy/core/downloader/contextfactory.py
浏览文件 @
30a27eff
from
OpenSSL
import
SSL
from
twisted.internet.ssl
import
ClientContextFactory
try
:
# available since twisted 14.0
from
twisted.internet._sslverify
import
ClientTLSOptions
except
ImportError
:
ClientTLSOptions
=
None
class
ScrapyClientContextFactory
(
ClientContextFactory
):
"A SSL context factory which is more permissive against SSL bugs."
# see https://github.com/scrapy/scrapy/issues/82
# and https://github.com/scrapy/scrapy/issues/26
# and https://github.com/scrapy/scrapy/issues/981
def
__init__
(
self
):
# see this issue on why we use TLSv1_METHOD by default
# https://github.com/scrapy/scrapy/issues/194
self
.
method
=
SSL
.
TLSv1_METHOD
def
getContext
(
self
,
hostname
=
None
,
port
=
None
):
ctx
=
ClientContextFactory
.
getContext
(
self
)
# Enable all workarounds to SSL bugs as documented by
# http://www.openssl.org/docs/ssl/SSL_CTX_set_options.html
ctx
.
set_options
(
SSL
.
OP_ALL
)
if
hostname
and
ClientTLSOptions
is
not
None
:
# workaround for TLS SNI
ClientTLSOptions
(
hostname
,
ctx
)
return
ctx
from
scrapy
import
twisted_version
if
twisted_version
>=
(
14
,
0
,
0
):
from
twisted.web.client
import
BrowserLikePolicyForHTTPS
from
twisted.internet.ssl
import
optionsForClientTLS
class
ScrapyClientContextFactory
(
BrowserLikePolicyForHTTPS
):
"""
Using Twisted recommended context factory for twisted.web.client.Agent
Quoting:
"The default is to use a BrowserLikePolicyForHTTPS,
so unless you have special requirements you can leave this as-is."
See http://twistedmatrix.com/documents/current/api/twisted.web.client.Agent.html
"""
class
OpenSSLMethodContextFactory
(
ScrapyClientContextFactory
):
openssl_method
=
SSL
.
SSLv23_METHOD
def
creatorForNetloc
(
self
,
hostname
,
port
):
return
optionsForClientTLS
(
hostname
.
decode
(
"ascii"
),
trustRoot
=
self
.
_trustRoot
,
extraCertificateOptions
=
{
'method'
:
self
.
openssl_method
})
else
:
class
OpenSSLMethodContextFactory
(
ClientContextFactory
):
"A SSL context factory which is more permissive against SSL bugs."
# see https://github.com/scrapy/scrapy/issues/82
# and https://github.com/scrapy/scrapy/issues/26
# and https://github.com/scrapy/scrapy/issues/981
openssl_method
=
SSL
.
SSLv23_METHOD
def
__init__
(
self
):
self
.
method
=
self
.
openssl_method
def
getContext
(
self
,
hostname
=
None
,
port
=
None
):
ctx
=
ClientContextFactory
.
getContext
(
self
)
# Enable all workarounds to SSL bugs as documented by
# http://www.openssl.org/docs/ssl/SSL_CTX_set_options.html
ctx
.
set_options
(
SSL
.
OP_ALL
)
if
hostname
and
ClientTLSOptions
is
not
None
:
# workaround for TLS SNI
ClientTLSOptions
(
hostname
,
ctx
)
return
ctx
ScrapyClientContextFactory
=
OpenSSLMethodContextFactory
class
SSLv3ContextFactory
(
OpenSSLMethodContextFactory
):
openssl_method
=
SSL
.
SSLv3_METHOD
class
TLSv1ContextFactory
(
OpenSSLMethodContextFactory
):
openssl_method
=
SSL
.
TLSv1_METHOD
class
TLSv11ContextFactory
(
OpenSSLMethodContextFactory
):
openssl_method
=
SSL
.
TLSv1_1_METHOD
class
TLSv12ContextFactory
(
OpenSSLMethodContextFactory
):
openssl_method
=
SSL
.
TLSv1_2_METHOD
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录