Python“请求"库 - 定义特定的 DNS? [英] Python 'requests' library - define specific DNS?

查看:55
本文介绍了Python“请求"库 - 定义特定的 DNS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用 python requests 图书馆.

In my project I'm handling all HTTP requests with python requests library.

现在,我需要使用特定的 DNS 查询 http 服务器 - 有两个环境,每个环境都使用自己的 DNS,并且更改是独立进行的.

Now, I need to query the http server using specific DNS - there are two environments, each using its own DNS, and changes are made independently.

因此,当代码运行时,它应该使用特定于环境的 DNS,而不是我的互联网连接中指定的 DNS.

So, when the code is running, it should use DNS specific to the environment, and not the DNS specified in my internet connection.

有没有人用 python-requests 试过这个?我只找到了 urllib2 的解决方案:
https://stackoverflow.com/questions/4623090/python-set-自定义 dns-server-for-urllib-requests

Has anyone tried this using python-requests? I've only found solution for urllib2:
https://stackoverflow.com/questions/4623090/python-set-custom-dns-server-for-urllib-requests

推荐答案

requests 使用 urllib3,最终也使用 httplib.HTTPConnection,所以来自 https://stackoverflow.com/的技术questions/4623090/python-set-custom-dns-server-for-urllib-requests(现已删除,它仅链接到 告诉 urllib2 使用自定义 DNS) 在一定程度上仍然适用.

requests uses urllib3, which ultimately uses httplib.HTTPConnection as well, so the techniques from https://stackoverflow.com/questions/4623090/python-set-custom-dns-server-for-urllib-requests (now deleted, it merely linked to Tell urllib2 to use custom DNS) still apply, to a certain extent.

urllib3.connection 模块将 httplib.HTTPConnection 子类化为同名,将 .connect() 方法替换为一个调用self._new_conn.反过来,这委托给 urllib3.util.connection.create_connection().修补那个函数可能是最简单的:

The urllib3.connection module subclasses httplib.HTTPConnection under the same name, having replaced the .connect() method with one that calls self._new_conn. In turn, this delegates to urllib3.util.connection.create_connection(). It is perhaps easiest to patch that function:

from urllib3.util import connection


_orig_create_connection = connection.create_connection


def patched_create_connection(address, *args, **kwargs):
    """Wrap urllib3's create_connection to resolve the name elsewhere"""
    # resolve hostname to an ip address; use your own
    # resolver here, as otherwise the system resolver will be used.
    host, port = address
    hostname = your_dns_resolver(host)

    return _orig_create_connection((hostname, port), *args, **kwargs)


connection.create_connection = patched_create_connection

并且您需要提供自己的代码来将地址的 host 部分解析为 ip 地址,而不是依赖于 connection.create_connection() 调用(它包装 socket.create_connection()) 来为你解析主机名.

and you'd provide your own code to resolve the host portion of the address into an ip address instead of relying on the connection.create_connection() call (which wraps socket.create_connection()) to resolve the hostname for you.

像所有monkeypatching一样,注意代码在以后的版本中没有显着变化;这里的补丁是针对 urllib3 版本 1.21.1 创建的.但应该适用于早至 1.9 的版本.

Like all monkeypatching, be careful that the code hasn't significantly changed in later releases; the patch here was created against urllib3 version 1.21.1. but should work for versions as far back as 1.9.

请注意,此答案已重新编写以适用于较新的 urllib3 版本,这些版本添加了更方便的修补位置.查看旧方法的编辑历史,适用于版本<1.9,作为供应商 urllib3 版本的补丁而不是独立安装.

Note that this answer was re-written to work with newer urllib3 releases, which have added a much more convenient patching location. See the edit history for the old method, applicable to version < 1.9, as a patch to the vendored urllib3 version rather than a stand-alone installation.

这篇关于Python“请求"库 - 定义特定的 DNS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆