Skip to content

Instantly share code, notes, and snippets.

@Aviksaikat
Created March 14, 2023 11:22
Show Gist options
  • Save Aviksaikat/4bee131e44c063dc1d1bd2b3f7fcf95d to your computer and use it in GitHub Desktop.
Save Aviksaikat/4bee131e44c063dc1d1bd2b3f7fcf95d to your computer and use it in GitHub Desktop.
Redirect any incoming request
#!/usr/bin/python3
import sys
from http.server import HTTPServer, BaseHTTPRequestHandler
if len(sys.argv)-1 != 2:
print("""
Usage: {} <port_number> <url>
""".format(sys.argv[0]))
sys.exit()
class Redirect(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(302)
self.send_header('Location', sys.argv[2])
self.end_headers()
def send_error(self, code, message=None):
self.send_response(302)
self.send_header('Location', sys.argv[2])
self.end_headers()
HTTPServer(("", int(sys.argv[1])), Redirect).serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment