urllib open - how to control the number of retries

Posted by user1641071 on Stack Overflow See other posts from Stack Overflow or by user1641071
Published on 2012-09-01T21:36:16Z Indexed on 2012/09/01 21:37 UTC
Read the original article Hit count: 177

Filed under:
|

how can i control the number of retries of the "opener.open"?

for example, in the following code, it will send about 6 "GET" HTTP requests (i saw it in the Wireshark sniffer) before it goes to the " except urllib.error.URLError" success/no-success lines.

    password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None,url, username, password)
    handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
    opener = urllib.request.build_opener(handler)

    try: 
        resp = opener.open(url,None,1)

    except urllib.error.URLError as e:
        print ("no success")
    else:
        print ("success!")

© Stack Overflow or respective owner

Related posts about python-3.x

Related posts about urllib