When using URLLIB2 to request HTTP today, the timeout was found to be inconsistent with the settings.
For example, the following code httpreq.request (' http://www.baidu.com ', timeout=1), after the use of the 80 port Iptables sealed off, found to 2 seconds to time out, and set 1 does not match. Bag analysis has two connections, but puzzled why the connection two times, is the retry mechanism.
Tracked the code and found that it would eventually invoke socket.create_connection to create the socket,
The logic is as follows
def create_connection (address, timeout=_global_default_timeout,
source_address=none):
For RES in getaddrinfo (host, port, 0, Sock_stream):
af, Socktype, Proto, canonname, sa = res
sock = None
try:< C5/>sock = socket (AF, Socktype, proto)
if timeout is not _global_default_timeout:
sock.settimeout (timeout)
if source_address:
sock.bind (source_address)
sock.connect (SA) return
sockIt can be found that in Domain name resolution, the resolved multiple addresses are sequentially tried to connect until they succeed. The example of that domain has two IP, so will try to connect two times