forked from AparajitoSaha/ExPiRior
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
45 lines (40 loc) · 1.42 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
import socket
import threading as Thread
import time
import sys
class SocketServer():
def __init__(self, conn):
# conn represents client connection, initializes the TCP/IP connection
# between server on Pi and client on laptop
self.conn = conn
def run(self):
# Runs the server on the Pi, checking for incoming connections from
# client over wireless TCP/IP
global isConnected
print("Python server started")
while True:
cmd = ""
try:
print("Calling blocking conn.recv()")
msg = recvClientMessage()
except:
print("exception in conn.recv()")
# happens when connection is reset from the client
break
conn.close()
print("Client disconnected. Waiting for next client...")
isConnected = False
print("SocketServer terminated")
def sendClientMessage(self, msg):
# Sends a string message over to the client
msg = msg + "\0"
msg = bytes(msg, 'utf-8')
print("Message sent to client: ", msg)
self.conn.sendall(msg)
def recvClientMessage(self):
# receives message from client and passes it on to
# other code modules
msg = self.conn.recv(4096)
msg = msg.decode('utf-8')
print("Message received = ", msg[:-1])
return msg