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

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

问题描述

在我的项目中,我使用python 处理所有HTTP请求请求



现在,我需要使用特定的DNS查询http服务器 - 有两个环境,每个使用它自己的DNS和更改是独立的。



所以,当代码运行时,它应该使用特定于环境的DNS,而不是我的互联网连接中指定的DNS有人试过这个使用python请求吗?



我只找到了urllib2的解决方案:

https://stackoverflow.com/questions/4623090/python-set-custom-dns-server-for-urllib-requests

解决方案

请求使用 urllib3 ,最终使用 httplib.HTTPConnection 以及 https:// stackoverflow.com/questions/4623090/python-set-custom-dns-server-for-urllib-requests 在一定程度上仍然适用。



requests.packages.urllib3.connection 模块子类 httplib.HTTPConnection 同名,替换了 .connect()方法,一个调用 self._new_conn 。这可能是最简单的方式来补丁方法:

  from requests.packages.urllib3.connection import HTTPConnection 

def patched_new_conn(self):
建立套接字连接并设置nodelay设置

:return:一个新的套接字连接

#将主机名解析为ip地址;在这里使用你自己的
#解析器,否则将使用系统解析器。
hostname = your_dns_resolver(self.host)
try:
conn = socket.create_connection(
(hostname,self.port),
self.timeout,
self.source_address,

除了AttributeError:#Python 2.6
conn = socket.create_connection(
(hostname,self.port),
self.timeout ,

conn.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,
self.tcp_nodelay)
return conn

HTTPConnection._new_conn = patched_new_conn

,您将提供自己的代码来解决 self.host 到一个ip地址,而不是依靠 socket.create_connection()调用解析您的主机名。



像所有monkeypatching一样,注意代码在以后的版本中没有大的改变;这里的修补程序是针对请求2.3.0创建的。


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

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.

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

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 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 still apply, to a certain extent.

The requests.packages.urllib3.connection module subclasses httplib.HTTPConnection under the same name, having replaced the .connect() method with one that calls self._new_conn. It is perhaps easiest to patch that method:

from requests.packages.urllib3.connection import HTTPConnection

def patched_new_conn(self):
    """ Establish a socket connection and set nodelay settings on it

    :return: a new socket connection
    """
    # resolve hostname to an ip address; use your own
    # resolver here, as otherwise the system resolver will be used.
    hostname = your_dns_resolver(self.host)
    try:
        conn = socket.create_connection(
            (hostname, self.port),
            self.timeout,
            self.source_address,
        )
    except AttributeError: # Python 2.6
        conn = socket.create_connection(
            (hostname, self.port),
            self.timeout,
        )
    conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY,
                    self.tcp_nodelay)
    return conn

HTTPConnection._new_conn = patched_new_conn

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

Like all monkeypatching, be careful that the code hasn't significantly changed in later releases; the patch here was created against requests 2.3.0.

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

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