-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_socket.py
42 lines (31 loc) · 945 Bytes
/
server_socket.py
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
import socket
import sys
from _thread import *
HOST = '' # Symbolic name meaning all available interfaces
PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')
try:
s.bind((HOST, PORT))
except socket.error as msg:
print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
print('Socket bind complete')
s.listen(10)
print('Socket listening...')
def clientthread(conn):
conn.send(bytes('Welcome to the server!\n', 'utf-8'))
while True:
data = conn.recv(1024)
reply = 'OK...' + data.decode('utf-8')
if not data:
break
conn.sendall(bytes(reply, 'utf-8'))
conn.close()
# now keep talking with the client
while True:
# wait to accept a connection - blocking call
conn, addr = s.accept()
print('Connected with ' + addr[0] + ':' + str(addr[1]))
start_new_thread(clientthread, (conn,))
s.close()