main.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from http.server import BaseHTTPRequestHandler, HTTPServer
  2. import time
  3. import requests
  4. import sys
  5. import os
  6. arozRequestEndpoint = "http://localhost:8080/api/ajgi/interface"
  7. serverPort = 8000
  8. def getMime(filename):
  9. ext = filename.split(".").pop()
  10. if ext == "png" or ext == "jpg" or ext == "jpeg" or ext == "gif":
  11. return "image/" + ext
  12. elif ext == "html" or ext == "htm":
  13. return "text/" + ext
  14. return "application/" + ext
  15. def resolveVirtualPath(path, token):
  16. # Create an AGI script to perform a specific operation on ArozOS
  17. # For example, this script get the user's desktop path on host file system
  18. script = ""
  19. script += 'var abspath = decodeAbsoluteVirtualPath("' + path + '");'
  20. script += 'sendResp(abspath);'
  21. # Put the script into the POST request payload
  22. payload = {'script':script}
  23. # Post the script with the token to ArozOS
  24. session = requests.Session()
  25. resp = session.post(arozRequestEndpoint + "?token=" + token, data = payload)
  26. print(resp.content.decode('UTF-8'))
  27. # Return as a simple JSON string (Replace \\ with / for Windows)
  28. return '"' + str(resp.content.decode('UTF-8')).replace("\\", "/") + '"'
  29. class Router(BaseHTTPRequestHandler):
  30. def do_GET(self):
  31. # Get the request token and username from the request header
  32. username = self.headers.get('aouser') # This is the username of the requesting user
  33. token = self.headers.get('aotoken') # This is the token for requesting ArozOS for futher file operation / information
  34. print("Req: " + self.path + " by " + username)
  35. if self.path == "/":
  36. self.path = "/index.html"
  37. if self.path == "/api":
  38. # Demo for getting information from the ArozOS AGI gateway
  39. resp = resolveVirtualPath("user:/Desktop", token);
  40. self.send_response(200)
  41. self.send_header('Content-type',"application/json")
  42. self.end_headers()
  43. self.wfile.write(bytes(str(resp), "utf-8"))
  44. return
  45. if os.path.exists("web" + self.path):
  46. # Serve the file
  47. self.send_response(200)
  48. self.send_header('Content-type',getMime(self.path))
  49. self.end_headers()
  50. f = open("web" + self.path, 'rb')
  51. self.wfile.write(f.read())
  52. f.close()
  53. return
  54. else:
  55. self.send_response(404)
  56. self.send_header("Content-type", "text/html")
  57. self.wfile.write(bytes("404 Not Found", "utf-8"))
  58. return
  59. return
  60. self.send_response(200)
  61. self.send_header("Content-type", "text/html")
  62. self.end_headers()
  63. self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
  64. self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
  65. self.wfile.write(bytes("<body>", "utf-8"))
  66. self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8"))
  67. self.wfile.write(bytes("</body></html>", "utf-8"))
  68. if __name__ == "__main__":
  69. # Parse the input from ArozOS
  70. # ArozOS will pass in port and parent calling endpoint and parsed by the start.sh / start.bat
  71. # print(sys.argv)
  72. if (len(sys.argv) > 1):
  73. serverPort = int(sys.argv[1][1:])
  74. arozRequestEndpoint = sys.argv[2]
  75. webServer = HTTPServer(("localhost", serverPort), Router)
  76. print("PyServer started http://%s:%s" % ("localhost", serverPort))
  77. try:
  78. webServer.serve_forever()
  79. except KeyboardInterrupt:
  80. pass
  81. webServer.server_close()
  82. print("PyServer stopped.")