-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsocket_server.py
More file actions
32 lines (25 loc) · 779 Bytes
/
socket_server.py
File metadata and controls
32 lines (25 loc) · 779 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
def listen():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
s.bind(('127.0.0.01', 5983))
s.listen(5)
while True:
connection, address = s.accept()
print('connection:::', connection)
print('address:::', address)
try:
connection.settimeout(5)
buf = connection.recv(1024)
print('buf::', buf)
print('str:', bytes.decode(buf))
if buf == '1':
connection.send(str.encode('welcome to server!'))
else:
connection.send(str.encode('.'))
except socket.timeout:
print('time out')
connection.close()
if __name__ == '__main__':
print('begin...')
listen()
print('end...')