编程语言
首页 > 编程语言> > 如何通过python请求模块发送请求时选择特定的密码

如何通过python请求模块发送请求时选择特定的密码

作者:互联网

用例:我想知道主机名与python请求模块支持多少密码.

我无法找到一种方法来提供请求模块挂钩的密码名称.任何人都可以建议提供指定密码的方式.

import ssl

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager


class Ssl3HttpAdapter(HTTPAdapter):
    """"Transport adapter" that allows us to use SSLv3."""

    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(
            num_pools=connections, maxsize=maxsize,
            block=block, ssl_version=ssl.PROTOCOL_SSLv3)

解决方法:

如果您使用的是请求版本2.12.0,则会在Configuring TLS With Requests上发布一篇博文,其中介绍了允许您配置SSLContext的新功能(请注意,此博客文章是在OP提出问题后编写的):

The feature added in Requests v2.12.0 is that urllib3 now accepts an
SSLContext object in the constructors for ConnectionPool objects. This
SSLContext will be used as the factory for the underlying TLS
connection, and so all settings applied to it will also be applied to
those low-level connections.

The best way to do this is to use the SSLContext factory function
requests.packages.urllib3.util.ssl_.create_urllib3_context. This is
analogous to Python’s ssl.create_default_context function but applies
the more-strict default TLS configuration that Requests and urllib3
both use. This function will return an SSLContext object that can then
have further configuration applied. On top of that, the function also
takes a few arguments to allow overriding default configuration.

To provide the new SSLContext object, you will need to write a
TransportAdapter that is appropriate for the given host.

以下示例代码作为如何使用此方法在请求中重新启用3DES的示例.

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.ssl_ import create_urllib3_context

# This is the 2.11 Requests cipher string, containing 3DES.
CIPHERS = (
    'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:'
    'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:'
    '!eNULL:!MD5'
)


class DESAdapter(HTTPAdapter):
    """
    A TransportAdapter that re-enables 3DES support in Requests.
    """
    def init_poolmanager(self, *args, **kwargs):
        context = create_urllib3_context(ciphers=CIPHERS)
        kwargs['ssl_context'] = context
        return super(DESAdapter, self).init_poolmanager(*args, **kwargs)

    def proxy_manager_for(self, *args, **kwargs):
        context = create_urllib3_context(ciphers=CIPHERS)
        kwargs['ssl_context'] = context
        return super(DESAdapter, self).proxy_manager_for(*args, **kwargs)

s = requests.Session()
s.mount('https://some-3des-only-host.com', DESAdapter())
r = s.get('https://some-3des-only-host.com/some-path')

还有一个hack可能,您可以在github pages for the requests modulehttps://stackoverflow.com/a/32651967/2364215阅读,但它修改了底层库代码,我不推荐它(请求模块的作者也没有,正如您将在该页面上找到的那样) .另一方面,如果您使用较旧的请求包并且无法升级,则可能是您的最佳选择.它等于覆盖urllib3模块的DEFAULT_CIPHERS:

requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += ':RC4-SHA' 

如果您有其他代码在执行修改后将使用请求模块,但不需要修改,您可能希望将DEFAULT_CIPHERS恢复为其先前的值.

标签:python,python-3-x,https,python-requests,pyopenssl
来源: https://codeday.me/bug/20190623/1267123.html