-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
41 lines (32 loc) · 985 Bytes
/
server.py
File metadata and controls
41 lines (32 loc) · 985 Bytes
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
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
IP = 'localhost'
PORT = 9000
received = {}
class requestHandler(BaseHTTPRequestHandler):
def do_GET (self):
global received
if received:
self.send_status_code(200)
# Return ok if data already exists and then send data
self.wfile.write(received)
else:
self.send_status_code(400)
# Return not ok if data are not available yet
def do_POST(self):
global received
content_length = int(self.headers.get('content-length', 0))
# Get the length of the contents
received = self.rfile.read(content_length)
self.send_status_code(200)
def send_status_code(self, status_code):
self.send_response(status_code)
self.send_header('content-type', 'application/json')
self.end_headers()
def main():
server_address = (IP, PORT)
server = HTTPServer(server_address, requestHandler)
print('Server running on port %s' % PORT)
server.serve_forever()
if __name__ == '__main__':
main()