简体   繁体   English

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

[英]how to force python httplib library to use only A requests

The problem is that urllib using httplib is querying for AAAA records. 问题在于使用httplib的urllib正在查询AAAA记录。

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]

The correct answer is: 正确答案是:

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

The Python socket library is using the following: Python套接字库正在使用以下代码:

socket.socket([family[, type[, proto]]]) Create a new socket using the given address family, socket type and protocol number. socket.socket([family [,type [,proto]]])使用给定的地址族,套接字类型和协议号创建一个新的套接字。 The address family should be AF_INET (the default), AF_INET6 or AF_UNIX. 地址族应为AF_INET(默认),AF_INET6或AF_UNIX。 The socket type should be SOCK_STREAM (the default), SOCK_DGRAM or perhaps one of the other SOCK_ constants. 套接字类型应为SOCK_STREAM(默认值),SOCK_DGRAM或其他SOCK_常量之一。 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. 默认情况下,它使用0,如果用2调用,它将仅查询A记录。

Remember caching the resolv results in your app IS A REALLY BAD IDEA. 请记住,在您的应用程序中缓存解析结果是一个非常糟糕的想法。 Never do it! 永远不要做!

Look here: how-do-i-resolve-an-srv-record-in-python 看这里: 如何解决srv记录在python中

Once you resolved the correct A ip, use it in your request, instead of the dns. 解决正确的A ip后,请在请求中使用它,而不是dns。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM