-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotArmMiniFirmataHTTPServer.py
49 lines (35 loc) · 1.93 KB
/
RobotArmMiniFirmataHTTPServer.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
import socket #Needed to send and receive network control communications.
#More info: https://wiki.python.org/moin/UdpCommunication
import threading #Used for concurrent robot communication with other processes.
#More info: https://docs.python.org/2/library/threading.html
import time #Used to create delays for proper timing of robot actions.
#More info: https://docs.python.org/2/library/time.html
import sys #Used for certain specific calls to the system.
#More info: https://docs.python.org/2/library/sys.html
import asyncio #Another type of threading for asynchronous multi-processing.
#More info: https://docs.python.org/3/library/asyncio-task.html
import json #Needed to format robot control messages.
#More info: https://docs.python.org/3/library/json.html
import pyfirmata #Needed to talk to Arduino directly with Standard Firmata.
#More info on Python: https://pypi.org/project/pyFirmata/
#More info on Arduino: https://www.arduino.cc/en/Reference/Firmata
#More info on Firmata: http://firmata.org/wiki/Main_Page
from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'Hello, world!')
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
response = BytesIO()
response.write(b'This is POST request. ')
response.write(b'Received: ')
response.write(body)
self.wfile.write(response.getvalue())
httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()