-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
171 lines (150 loc) · 6.29 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Filename: server.py
# Created By: Mackenly Jones on 07/12/2022
# Web: mackenly.com
# Twitter: @mackenlyjones
import os
import argparse
import logging
import json
import PyATEMMax
from api.Api import Api
from http.server import *
# create the switcher object
switcher = PyATEMMax.ATEMMax()
api = Api(switcher)
passphrase = ''
ip = ''
class GFG(BaseHTTPRequestHandler):
# on GET...
def do_GET(self):
global passphrase, ip
if self.headers.get('Authorization') == passphrase or passphrase == '':
# http response code - success
self.send_response(200)
# http header - content type - html
self.send_header('Content-type', 'application/json')
# CORS headers
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Authorization')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
# http body - json
self.wfile.write(bytes(str(json.dumps(api.get(self.path, passphrase, ip))), "utf8"))
log.info("GET request received, sent response")
else:
# http response code - unauthorized
self.send_response(401)
# http header - content type - html
self.send_header('Content-type', 'application/json')
# CORS headers
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Authorization')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
# http body - json
self.wfile.write(b'{"error": "unauthorized"}')
log.error("GET request received, unauthorized")
return
# on POST...
def do_POST(self):
global passphrase, ip
if self.headers.get('Authorization') == passphrase or passphrase == '':
# http response code - success
self.send_response(200)
# http header - content type - html
self.send_header('Content-type', 'application/json')
# CORS headers
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Authorization')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
# http body - json
self.wfile.write(bytes(str(json.dumps(api.post(self.path, passphrase, ip))), "utf8"))
log.info("POST request received, sent response")
else:
# http response code - unauthorized
self.send_response(401)
# http header - content type - html
self.send_header('Content-type', 'application/json')
# CORS headers
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Authorization')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
# http body - json
self.wfile.write(b'{"error": "unauthorized"}')
log.error("POST request received, unauthorized")
return
# on OPTIONS...
def do_OPTIONS(self):
global passphrase, ip
# http response code - success
self.send_response(200)
# http header - content type - html
self.send_header('Content-type', 'application/json')
# CORS headers
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Authorization')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
# http body - json
self.wfile.write(bytes(str(json.dumps(api.options(self.path, passphrase, ip))), "utf8"))
log.info("OPTIONS request received, sent response")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--ip', help='switcher IP address')
parser.add_argument('--passphrase', help='passphrase to compare requests to', type=str, default='')
parser.add_argument('--port', help='port to start server on', type=int, default=5555)
args = parser.parse_args()
# set global variables
global passphrase, ip
passphrase = os.environ.get('PASSPHRASE', args.passphrase)
ip = os.environ.get('SERVER_IP', args.ip)
preferred_port = os.environ.get('PORT', args.port)
# if no ip is provided, exit
if ip is None:
log.error("No IP address provided")
exit()
if passphrase is None:
log.info("No passphrase provided, using no passphrase")
passphrase = ''
if preferred_port is None:
# if the default port is not
log.info("No port provided, using default port 5555")
preferred_port = 5555
log.info("Initializing switcher")
switcher.setLogLevel(logging.INFO) # Set switcher verbosity (try DEBUG to see more)
switcher.connect(ip)
log.info("Waiting for connection")
switcher.waitForConnection()
log.info("Connected to switcher")
# this is the object which take port
# number and the server-name
# for running the server
port = ThreadingHTTPServer(('', preferred_port), GFG)
# this is used for running our
# server as long as we wish
# i.e. forever
log.info("Starting API server on http://localhost:" + str(port.server_port))
port.serve_forever()
# setup logging
logging.basicConfig( datefmt='%H:%M:%S',
level=logging.INFO,
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s')
log = logging.getLogger('PyATEMAPI')
# run main to start the connection and server
try:
main()
except KeyboardInterrupt:
log.info("Exiting from keyboard interrupt")
switcher.disconnect()
exit()
except Exception as e:
log.error("An error occurred: " + str(e))
switcher.disconnect()
exit()