如何强制 python httplib 库仅使用 A 请求

发布于 2024-08-07 02:27:21 字数 827 浏览 3 评论 0原文

问题是 urllib 使用 httplib 正在查询 AAAA 记录。

我想避免这种情况。有什么好的方法可以做到这一点吗?

>>> import socket
>>> socket.gethostbyname('www.python.org')
'82.94.164.162'


21:52:37.302028 IP 192.168.0.9.44992 > 192.168.0.1.53: 27463+ A? www.python.org. (32)
21:52:37.312031 IP 192.168.0.1.53 > 192.168.0.9.44992: 27463 1/0/0 A 82.94.164.162 (48)


 python /usr/lib/python2.6/urllib.py -t http://www.python.org >/dev/null 2>&1

 21:53:44.118314 IP 192.168.0.9.40669 > 192.168.0.1.53: 32354+ A? www.python.org. (32)
21:53:44.118647 IP 192.168.0.9.40669 > 192.168.0.1.53: 50414+ AAAA? www.python.org. (32)
21:53:44.122547 IP 192.168.0.1.53 > 192.168.0.9.40669: 32354 1/0/0 A 82.94.164.162 (48)
21:53:44.135215 IP 192.168.0.1.53 > 192.168.0.9.40669: 50414 1/0/0 AAAA[|domain]

The problem is that urllib using httplib is querying for AAAA records.

I would like to avoid that. Is there a nice way to do that?

>>> import socket
>>> socket.gethostbyname('www.python.org')
'82.94.164.162'


21:52:37.302028 IP 192.168.0.9.44992 > 192.168.0.1.53: 27463+ A? www.python.org. (32)
21:52:37.312031 IP 192.168.0.1.53 > 192.168.0.9.44992: 27463 1/0/0 A 82.94.164.162 (48)


 python /usr/lib/python2.6/urllib.py -t http://www.python.org >/dev/null 2>&1

 21:53:44.118314 IP 192.168.0.9.40669 > 192.168.0.1.53: 32354+ A? www.python.org. (32)
21:53:44.118647 IP 192.168.0.9.40669 > 192.168.0.1.53: 50414+ AAAA? www.python.org. (32)
21:53:44.122547 IP 192.168.0.1.53 > 192.168.0.9.40669: 32354 1/0/0 A 82.94.164.162 (48)
21:53:44.135215 IP 192.168.0.1.53 > 192.168.0.9.40669: 50414 1/0/0 AAAA[|domain]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-08-14 02:27:21

正确答案是:

http://docs.python.org/library/socket.html

Python 套接字库使用以下内容:

socket.socket([family[, type[, proto]]])
使用给定的地址族、套接字类型和协议号创建一个新套接字。地址族应为 AF_INET(默认值)、AF_INET6 或 AF_UNIX。套接字类型应该是 SOCK_STREAM(默认)、SOCK_DGRAM 或其他 SOCK_ 常量之一。协议号通常为零,在这种情况下可以省略。

/* Supported address families. */
#define AF_UNSPEC       0
#define AF_INET         2       /* Internet IP Protocol         */
#define AF_INET6        10      /* IP version 6                 */

默认情况下它使用 0,如果您使用 2 调用它,它将仅查询 A 记录。

请记住,在应用程序中缓存解析结果是一个非常糟糕的主意。永远不要这样做!

The correct answer is:

http://docs.python.org/library/socket.html

The Python socket library is using the following:

socket.socket([family[, type[, proto]]])
Create a new socket using the given address family, socket type and protocol number. The address family should be AF_INET (the default), AF_INET6 or AF_UNIX. The socket type should be SOCK_STREAM (the default), SOCK_DGRAM or perhaps one of the other SOCK_ constants. The protocol number is usually zero and may be omitted in that case.

/* Supported address families. */
#define AF_UNSPEC       0
#define AF_INET         2       /* Internet IP Protocol         */
#define AF_INET6        10      /* IP version 6                 */

By default it is using 0 and if you call it with 2 it will query only for A records.

Remember caching the resolv results in your app IS A REALLY BAD IDEA. Never do it!

似狗非友 2024-08-14 02:27:21

看这里:how-do-i-resolve-an-srv -record-in-python

解析正确的 A ip 后,在请求中使用它,而不是 dns。

Look here: how-do-i-resolve-an-srv-record-in-python

Once you resolved the correct A ip, use it in your request, instead of the dns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文