Skip to content

Commit 44f9fa2

Browse files
committed
Add timeout_socket_server.py
This is a UNIX domain socket server that accepts connections and never responds, so that we can test timeouts on the client side.
1 parent 073b549 commit 44f9fa2

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

timeout_socket_server.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import socket
2+
import time
3+
import os
4+
5+
server_address = './uds_socket'
6+
7+
# Make sure the socket does not already exist
8+
try:
9+
os.unlink(server_address)
10+
except OSError:
11+
if os.path.exists(server_address):
12+
raise
13+
14+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
15+
print('starting up on {}'.format(server_address))
16+
sock.bind(server_address)
17+
sock.listen(1)
18+
19+
while True:
20+
print('\nwaiting for a connection')
21+
22+
connection, client_address = sock.accept()
23+
connection.settimeout(300)
24+
25+
try:
26+
print('connected')
27+
connect_time = time.time()
28+
while True:
29+
try:
30+
data = connection.recv(640)
31+
if not data:
32+
elapsed_time = int(time.time() - connect_time)
33+
print(f'no data after {elapsed_time}s')
34+
break
35+
except socket.timeout as exc:
36+
elapsed_time = int(time.time() - connect_time)
37+
print(f'timeout after {elapsed_time}s on recv')
38+
break
39+
finally:
40+
connection.close()

0 commit comments

Comments
 (0)