-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecond_socket_handler.py
58 lines (45 loc) · 1.48 KB
/
second_socket_handler.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
46
47
48
49
50
51
52
53
54
55
56
57
58
import time
import sys
import json
import ServoControl as SC
import SocketServer as sws
#init motors
pwm = SC.initPWM()
servo0 = SC.Servo(pwm, 0, 200)
servo1 = SC.Servo(pwm, 1, 90)
servo2 = SC.Servo(pwm, 2, 200)
servo2.setDegree(90)
servo0.setDegree(1)
servo1.setDegree(10)
class MyTCPHandler(sws.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
enabled = True
def handle(self):
if self.enabled:
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print "{} wrote:".format(self.client_address[0])
print self.data
data = json.loads(self.data)
self.request.sendall(self.data.upper())
servo2.setDegree(int(data['x']))
servo0.setDegree(int(data['z']))
servo1.setDegree(int(data['y']))
if __name__ == "__main__":
HOST, PORT = "localhost", 8888
# Create the server, binding to localhost on port 9999
server = sws.TCPServer((HOST, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
try:
server.serve_forever()
except KeyboardInterrupt:
servo2.setDegree(90)
servo0.setDegree(1)
servo1.setDegree(10)
sys.exit()