forked from nicolashahn/whiteboard-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
45 lines (37 loc) · 1.23 KB
/
server.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
43
44
45
#!/usr/bin/env python3
import json
import socket
import threading
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
class ClientThread(threading.Thread):
def __init__(self, addr, conn):
threading.Thread.__init__(self)
self.conn = conn
print("New connection added: ", addr)
def run(self):
# self.csocket.send(bytes("Hi, This is from Server..",'utf-8'))
messages = []
message = b""
while True:
data = self.conn.recv(1024)
chunks = data.split(b"\n")
for chunk in chunks[:-1]:
message += chunk
messages.append(json.loads(message))
message = b""
message += chunks[-1]
print(messages)
if not data:
break
conn.sendall(b"hi\n")
conn.close()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
print("server started")
while True:
s.listen()
conn, addr = s.accept()
newthread = ClientThread(addr, conn)
newthread.start()