gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:56:08 26 4
gpt4 key购买 nike

用例:我想用 python 请求模块找出主机名支持多少密码。

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

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')

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

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

如果您有其他代码将在修改后使用 requests 模块,但不需要修改,您可能希望将 DEFAULT_CIPHERS 恢复为其之前的值。

关于python - 如何在通过 python 请求模块发送请求时选择特定的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40373115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com