Created
March 14, 2023 11:22
-
-
Save Aviksaikat/4bee131e44c063dc1d1bd2b3f7fcf95d to your computer and use it in GitHub Desktop.
Redirect any incoming request
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
#!/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