Problems with timeout settings for sockets in Python __python

Source: Internet
Author: User

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
            sock
It 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.