-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
36 lines (30 loc) · 1.41 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
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
import json
from NLP.bot import get_response
class MyHandler(SimpleHTTPRequestHandler):
def do_OPTIONS(self):
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*') # Allow access from any origin
self.send_header('Access-Control-Allow-Methods', 'POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
def do_POST(self):
if self.path == '/api/call_python_function':
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
input_data = json.loads(post_data.decode('utf-8'))
input_value = input_data["input"]
output_value = get_response(input_value)
output_data = {'result': output_value}
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*') # Allow access from any origin
self.end_headers()
self.wfile.write(json.dumps(output_data).encode('utf-8'))
else:
super().do_GET()
if __name__ == '__main__':
with TCPServer(('', 5001), MyHandler) as httpd:
print('Server listening on port 5001...')
httpd.serve_forever()