Created
March 12, 2023 07:48
-
-
Save vwrs/68d47c974c7b772f342ebfe9a1f88d70 to your computer and use it in GitHub Desktop.
A simple python server for a Unity WebGL build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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.
My script is just using OPs script to serve it. So it works all the same.
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
@mfbru Same! python -m http.server was fine but ran into the occasional issue with the game not packaging correctly. This version is great for that.
I'll share what I created with this
itch.io auto downloader just pass in the URL to the game. If you pass in the same URL/name and it has already been downloaded it will just launch it directly.
I got the auth token from looking at the existing itch.io client traffic