-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_eagain_error_handling.py
More file actions
81 lines (61 loc) · 2.11 KB
/
socket_eagain_error_handling.py
File metadata and controls
81 lines (61 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import errno
import select
import socket
import time
HOST = "example.com"
PORT = 80
REQUEST = b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n"
CONNECT_TIMEOUT = 5
READ_TIMEOUT = 5
MAX_RETRIES = 3
BUFFER_SIZE = 4096
def wait_for_socket(sock, wait_for_write=False, timeout=READ_TIMEOUT):
readers = [] if wait_for_write else [sock]
writers = [sock] if wait_for_write else []
readable, writable, _ = select.select(readers, writers, [], timeout)
return bool(writable if wait_for_write else readable)
def connect_socket():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(False)
try:
sock.connect((HOST, PORT))
except BlockingIOError:
pass
if not wait_for_socket(sock, wait_for_write=True, timeout=CONNECT_TIMEOUT):
sock.close()
raise TimeoutError(f"Timed out while connecting to {HOST}:{PORT}")
connect_error = sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
if connect_error != 0:
sock.close()
raise OSError(connect_error, f"Socket connect failed for {HOST}:{PORT}")
return sock
def read_response(sock):
response_chunks = []
retries = 0
while True:
try:
chunk = sock.recv(BUFFER_SIZE)
if not chunk:
break
response_chunks.append(chunk)
retries = 0
except BlockingIOError as exc:
if exc.errno not in (errno.EAGAIN, errno.EWOULDBLOCK):
raise
retries += 1
if retries > MAX_RETRIES:
raise TimeoutError("Socket stayed unavailable after repeated retries.") from exc
if not wait_for_socket(sock, timeout=READ_TIMEOUT):
raise TimeoutError("Timed out while waiting for the socket to become readable.") from exc
time.sleep(0.2)
return b"".join(response_chunks)
def main():
sock = connect_socket()
try:
sock.sendall(REQUEST)
response = read_response(sock)
print(response.decode("utf-8", errors="replace"))
finally:
sock.close()
if __name__ == "__main__":
main()