Python 3.7 how to fix longer time to download when increasing timeout in urllib.request.urlopen when in my company VPN

Viewed 168

I am having a hard time figuring out what the timeout argument actually does in urllib.request.urlopen.

When I am connected to the internet through my company VPN, the higher the value I set for timeout, the longer it takes to receive data from the server.

Below is an example, where I use urllib.request.urlopen to download the Wikipedia logo, with different timeout values.

When I am connected to my company VPN it takes 2seconds when I set timeout=1, approx 30 sec when I use timeout=30 and approximately 60 seconds when I don't set the timeout.

However if I run the same script without the VPN, then the downloading times are faster (0.4 sec approximately) and do not change with the different timeout values.

# Run on Mac OS with
#
# Python 3.7.6 (default, Feb  3 2020, 19:33:41)
# Type 'copyright', 'credits' or 'license' for more information
# IPython 7.7.0 -- An enhanced Interactive Python. Type '?' for help.
# PyDev console: using IPython 7.7.0
# Python 3.7.6 (default, Feb  3 2020, 19:33:41)
# [Clang 11.0.0 (clang-1100.0.33.16)] on darwin

import time
import datetime
import urllib.request

img_url = 'https://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png'

ts = time.time()
with urllib.request.urlopen(img_url, timeout=1) as response:
    img=response.read()
te = time.time()
print(f"Elapsed time, timeout = 1: {datetime.timedelta(seconds=te-ts)}")

ts = time.time()
with urllib.request.urlopen(img_url, timeout=30) as response:
    img=response.read()
te = time.time()
print(f"Elapsed time, timeout = 30: {datetime.timedelta(seconds=te-ts)}")
ts = time.time()

with urllib.request.urlopen(img_url) as response:
    img=response.read()
te = time.time()
print(f"Elapsed time, default timeout: {datetime.timedelta(seconds=te-ts)}")

How can I debug what is going on and how could I fix it?

0 Answers
Related