Over the last few months (and probably for quite a while before then too), a few issues have been raised on the Requests GitHub page asking how to select the version of SSL used by Requests. This is actually simple once you know how, so I thought I’d write a short post to show you how it’s done.
在过去的几个月中(可能还有很长一段时间), Requests GitHub页面上出现了一些问题,询问如何选择Requests使用的SSL版本。 一旦您知道如何做,这实际上很简单,所以我想写一篇简短的文章向您展示它是如何完成的。
A quick note before we begin: it is not possible to select the version of SSL you want to use before Requests v1.0.0 without changing the underlying library code. The following set of instructions will not work if you’re running an earlier version.
在开始之前的简短提示: 在不更改基础库代码的情况下,无法在Requests v1.0.0之前选择要使用的SSL版本 。 如果您运行的是早期版本,则以下说明将不起作用。
怎么做的 (How It’s Done)
Altogether this is relatively simple. To change the SSL version used in HTTPS, you are expected to subclass the HTTPAdapter
class and mount it to a Session
object. If, for example, you wanted to force the use of TLSv1, your new Transport Adapter will look like this:
总体而言,这相对简单。 要更改HTTPS中使用的SSL版本,您应该将HTTPAdapter
类子类化并将其安装到Session
对象。 例如,如果您想强制使用TLSv1,则新的传输适配器将如下所示:
from from requests.adapters requests.adapters import import HTTPAdapter
HTTPAdapter
from from requests.packages.urllib3.poolmanager requests.packages.urllib3.poolmanager import import PoolManag