Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
902 views
in Technique[技术] by (71.8m points)

dns - how to force python httplib library to use only A requests

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...