Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zztalker/dff1852a97c8aa52ce3efc8c79a1f5de to your computer and use it in GitHub Desktop.
Save zztalker/dff1852a97c8aa52ce3efc8c79a1f5de to your computer and use it in GitHub Desktop.
Python urllib timeout usage
  • Python 3.6.9
pz@asus:~/snippets/urllib-timeout $ python3 urllib_check.py
Test server send bytes at one moment, with timeout 1.
b'1234567890'
Duration: 0.0029508500010706484
Test server send 1 byte per 0.5 sec, with timeout 1.
b'1234567890'
Duration: 13.017826942999818
Test server send 1 bytes per 2 sec for total 10 bytes (56 sec long), with timeout 10.
b'1234567890'
Duration: 52.54494177000015
  • Python 3.7.7
pz@asus:~/snippets/urllib-timeout $ python3.7 urllib_check.py
Test server send bytes at one moment, with timeout 1.
b'1234567890'
Duration: 0.005291239001962822
Test server send 1 byte per 0.5 sec, with timeout 1.
b'1234567890'
Duration: 13.016211187998124
Test server send 1 bytes per 2 sec for total 10 bytes (56 sec long), with timeout 10.
b'1234567890'
Duration: 52.54022990700469
  • Python 3.8
pz@asus:~/snippets/urllib-timeout $ python3.8 urllib_check.py
Test server send bytes at one moment, with timeout 1.
b'1234567890'
Duration: 0.004608458002621774
Test server send 1 byte per 0.5 sec, with timeout 1.
b'1234567890'
Duration: 13.018239862001792
Test server send 1 bytes per 2 sec for total 10 bytes (56 sec long), with timeout 10.
b'1234567890'
Duration: 52.542847764001635
import socket
import time
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.bind(("0.0.0.0", 8888))
serv.listen(5)
while True:
conn, addr = serv.accept()
from_client = ""
data = conn.recv(4096)
if not data:
break
from_client += data.decode()
print(from_client)
if "longlasting" in from_client:
for b in """HTTP/1.1 200 OK\n\n1234567890""":
conn.send(b.encode())
time.sleep(2)
elif "wait" in from_client:
for b in """HTTP/1.1 200 OK\n\n1234567890""":
conn.send(b.encode())
time.sleep(0.5)
else:
conn.send("HTTP/1.1 200 OK\n\n1234567890".encode())
conn.close()
print("client disconnected")
import urllib.request
import timeit
import socket
def test_longlasting(url, timeout=None):
with urllib.request.urlopen(
f"http://127.0.0.1:8888/{url}", timeout=timeout
) as f:
print(f.read(10))
print("Test server send bytes at one moment, with timeout 1.")
start_time = timeit.default_timer()
try:
test_longlasting("timeout", 1)
except socket.timeout:
print("timeout")
print("Duration:", timeit.default_timer() - start_time)
print("Test server send 1 byte per 0.5 sec, with timeout 1.")
start_time = timeit.default_timer()
try:
test_longlasting("wait", 1)
except socket.timeout:
print("timeout")
print("Duration:", timeit.default_timer() - start_time)
print(
"Test server send 1 byte per 2 secs for total 10 bytes (56 sec long), "
"with timeout 10."
)
start_time = timeit.default_timer()
try:
test_longlasting("longlasting", 10)
except socket.timeout:
print("timeout")
print("Duration:", timeit.default_timer() - start_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment