Skip to content

Instantly share code, notes, and snippets.

@vwrs
Created March 12, 2023 07:48
Show Gist options
  • Save vwrs/68d47c974c7b772f342ebfe9a1f88d70 to your computer and use it in GitHub Desktop.
Save vwrs/68d47c974c7b772f342ebfe9a1f88d70 to your computer and use it in GitHub Desktop.
A simple python server for a Unity WebGL build
import sys
from http.server import SimpleHTTPRequestHandler, HTTPServer
class GzipRequestHandler(SimpleHTTPRequestHandler):
'''HTTPRequestHandler for gzip files'''
def end_headers(self):
'''Set Content-Encoding: gzip for gzipped files'''
if self.path.endswith('.gz'):
self.send_header('Content-Encoding', 'gzip')
super().end_headers()
def do_GET(self):
'''Set Content-Encoding and Content-Type to gzipped files'''
path = self.translate_path(self.path)
if path.endswith('.js.gz'):
with open(path, 'rb') as f:
content = f.read()
self.send_response(200)
self.send_header('Content-Type', 'application/javascript')
self.end_headers()
self.wfile.write(content)
elif path.endswith('.wasm.gz'):
with open(path, 'rb') as f:
content = f.read()
self.send_response(200)
self.send_header('Content-Type', 'application/wasm')
self.end_headers()
self.wfile.write(content)
elif path.endswith('.gz'):
with open(path, 'rb') as f:
content = f.read()
self.send_response(200)
self.send_header('Content-Type',self.guess_type(path))
self.end_headers()
self.wfile.write(content)
else:
super().do_GET()
def serve(port: int):
'''Run a local HTTP server'''
httpd = HTTPServer(('localhost', port), GzipRequestHandler)
print(f"Serving at http://localhost:{port}")
httpd.serve_forever()
if __name__ == "__main__":
try:
if len(sys.argv) != 2:
print(f'usage: {sys.argv[0]} [PORT]')
port = int(sys.argv[1])
serve(port)
except Exception as e:
print('Error:', e)
@mfbru
Copy link

mfbru commented Oct 22, 2024

Cool! I see you made the URL an argument to the script, it's a good idea because OP's script only works locally as it serves the website at localhost, so you can't access it from another client on the network.

@parkerlreed
Copy link

My script is just using OPs script to serve it. So it works all the same.

@mfbru
Copy link

mfbru commented Oct 25, 2024

Hello @vwrs, if you change "localhost" --> "0.0.0.0", the webserver can run from other network clients (e.g., smartphones) 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment