如何强制python httplib库仅使用A请求 [英] how to force python httplib library to use only A requests

查看:123
本文介绍了如何强制python httplib库仅使用A请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]


推荐答案

正确的答案是:

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

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

The Python socket library is using the following:

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

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                 */

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

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

记住在您的应用程序中缓存resolv结果是一个真正的BAD IDEA。不要这样做!

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

这篇关于如何强制python httplib库仅使用A请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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