-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.py
More file actions
56 lines (45 loc) · 1.67 KB
/
WebServer.py
File metadata and controls
56 lines (45 loc) · 1.67 KB
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
from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO
import json
import os
import subprocess as sp
# subprocess
workoutbotproc = None
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'OK')
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = str(self.rfile.read(content_length))
response = BytesIO()
global workoutbotproc
if os.environ['SLACK_VERIFICATION_TOKEN'] not in body:
response.write(b'Invalid request origin')
self.send_response(403)
self.end_headers()
self.wfile.write(response.getvalue())
return
if "stop" in body:
response.write(b'Workout lottery stopped')
if workoutbotproc is not None:
sp.Popen.terminate(workoutbotproc)
workoutbotproc = None
elif "start" in body:
if workoutbotproc is None:
response.write(b'Workout lottery started')
workoutbotproc = sp.Popen(['python3','slackbotExercise.py'])
else:
response.write(b'Workout lottery already running')
elif "status" in body:
response.write(b'Server status up and running')
else :
response.write(b'Unsupported command')
self.send_response(200)
self.end_headers()
self.wfile.write(response.getvalue())
port = os.environ.get('PORT', 5000)
httpd = HTTPServer(('0.0.0.0', port), SimpleHTTPRequestHandler)
print("Web Server Running")
httpd.serve_forever()