-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
31 lines (23 loc) · 840 Bytes
/
server.py
File metadata and controls
31 lines (23 loc) · 840 Bytes
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
import socket
import sys
srv_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = ('localhost', 8089)
try:
srv_socket.bind(addr)
except Exception as exc:
print(f"bind failed: {exc}")
sys.exit(1)
# https://docs.python.org/3/library/socket.html#socket.socket.listen
# Enable a server to accept connections. If backlog is specified, it must be at
# least 0 (if it is lower, it is set to 0); it specifies the number of
# unaccepted connections that the system will allow before refusing new
# connections. If not specified, a default reasonable value is chosen.
srv_socket.listen(5)
print(f"listen on address: {addr}")
while True:
conn, addr = srv_socket.accept()
buf = conn.recv(64)
if len(buf) > 1:
print(f"got data: {buf.decode()}")
else:
print(f"got 1 byte: {buf.decode()}")