-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
96 lines (82 loc) · 3.58 KB
/
main.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
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import requests
import sys
import os
arozRequestEndpoint = "http://localhost:8080/api/ajgi/interface"
serverPort = 8000
def getMime(filename):
ext = filename.split(".").pop()
if ext == "png" or ext == "jpg" or ext == "jpeg" or ext == "gif":
return "image/" + ext
elif ext == "html" or ext == "htm":
return "text/" + ext
return "application/" + ext
def resolveVirtualPath(path, token):
# Create an AGI script to perform a specific operation on ArozOS
# For example, this script get the user's desktop path on host file system
script = ""
script += 'var abspath = decodeAbsoluteVirtualPath("' + path + '");'
script += 'sendResp(abspath);'
# Put the script into the POST request payload
payload = {'script':script}
# Post the script with the token to ArozOS
session = requests.Session()
resp = session.post(arozRequestEndpoint + "?token=" + token, data = payload)
print(resp.content.decode('UTF-8'))
# Return as a simple JSON string (Replace \\ with / for Windows)
return '"' + str(resp.content.decode('UTF-8')).replace("\\", "/") + '"'
class Router(BaseHTTPRequestHandler):
def do_GET(self):
# Get the request token and username from the request header
username = self.headers.get('aouser') # This is the username of the requesting user
token = self.headers.get('aotoken') # This is the token for requesting ArozOS for futher file operation / information
print("Req: " + self.path + " by " + username)
if self.path == "/":
self.path = "/index.html"
if self.path == "/api":
# Demo for getting information from the ArozOS AGI gateway
resp = resolveVirtualPath("user:/Desktop", token);
self.send_response(200)
self.send_header('Content-type',"application/json")
self.end_headers()
self.wfile.write(bytes(str(resp), "utf-8"))
return
if os.path.exists("web" + self.path):
# Serve the file
self.send_response(200)
self.send_header('Content-type',getMime(self.path))
self.end_headers()
f = open("web" + self.path, 'rb')
self.wfile.write(f.read())
f.close()
return
else:
self.send_response(404)
self.send_header("Content-type", "text/html")
self.wfile.write(bytes("404 Not Found", "utf-8"))
return
return
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
self.wfile.write(bytes("<body>", "utf-8"))
self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8"))
self.wfile.write(bytes("</body></html>", "utf-8"))
if __name__ == "__main__":
# Parse the input from ArozOS
# ArozOS will pass in port and parent calling endpoint and parsed by the start.sh / start.bat
# print(sys.argv)
if (len(sys.argv) > 1):
serverPort = int(sys.argv[1][1:])
arozRequestEndpoint = sys.argv[2]
webServer = HTTPServer(("localhost", serverPort), Router)
print("PyServer started http://%s:%s" % ("localhost", serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("PyServer stopped.")