如何通过python请求模块发送请求时选择特定的密码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何通过python请求模块发送请求时选择特定的密码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3060字,纯文字阅读大概需要5分钟。
内容图文
![如何通过python请求模块发送请求时选择特定的密码](/upload/InfoBanner/zyjiaocheng/796/086e3d1cdce240e6bbeadd736c9168e1.jpg)
用例:我想知道主机名与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 module或https://stackoverflow.com/a/32651967/2364215阅读,但它修改了底层库代码,我不推荐它(请求模块的作者也没有,正如您将在该页面上找到的那样) .另一方面,如果您使用较旧的请求包并且无法升级,则可能是您的最佳选择.它等于覆盖urllib3模块的DEFAULT_CIPHERS:
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += ':RC4-SHA'
如果您有其他代码在执行修改后将使用请求模块,但不需要修改,您可能希望将DEFAULT_CIPHERS恢复为其先前的值.
内容总结
以上是互联网集市为您收集整理的如何通过python请求模块发送请求时选择特定的密码全部内容,希望文章能够帮你解决如何通过python请求模块发送请求时选择特定的密码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。