Eli Fulkerson .com HomeProjectsHttp-https-redirect
 

Python HTTP to HTTPS redirector

Description:

This is a small python script that runs forever, listening for HTTP connections and redirecting them to a specified URL. This was written for the express purpose of redirecting an HTTP connection to an HTTPS server, but will happily redirect to any URL. If the client application does not accept the redirection error code a basic HTML page is delivered which includes directions on how to reach the intended target.

Platform:

  • no platform specific code, should work anywhere that has Python
  • Language:

  • Python
  • HTML
  • Demonstrates:

  • basic python socket handling
  • http redirection codes
  • License:

  • I am placing this code (explicitly the file redirect.py.txt on this website) into the public domain. If you find it useful, I'd like to hear from you. If you build it into a larger application I would appreciate compensation and attribution, but am not requiring either.
  • Code:

    #!/usr/bin/python
    
    from socket import *
    from select import *
    
    HOST = ''
    PORT = 80
    TARGET = "https://www.yourserver.here"
    
    class Connection:
        """
        An accepted connection.
        """
        def __init__(self, socket, address):
            self.socket = socket
            self.ip = address[0]
    
        def sendline(self, data):
            self.socket.send(data + "\r\n")
    
        def fileno(self):
            return self.socket.fileno()
    
    class ConnectionHandler:
        """
        Maintains all connections.
        """
        def __init__(self):
            self.listener = socket(AF_INET, SOCK_STREAM)
            self.listener.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
            self.listener.bind((HOST, PORT))
            self.listener.listen(32)
            print "HTTP 302 Redirector is active and listening on port " + `PORT` + "."
    
        def run(self):
            """
            Listen for incoming connections on PORT.  When a connection is recieved, send a 302
            redirect and then close the socket.
            """
            # Accept an incoming connection.
            conn = select( [self.listener], [], [], 0.1)[0]
    
            if conn:
                socket, address = self.listener.accept()
                conn = Connection(socket, address)
    
                # burn some time so that the client and server don't have a timing error
                trash = conn.socket.recv(1024)
    
                # send our https redirect
                conn.sendline("HTTP/1.1 302 Encryption Required")
                conn.sendline("Location: " + TARGET)
                conn.sendline("Connection: close")
                conn.sendline("Cache-control: private")
                conn.sendline("")
                conn.sendline("<html><body>Encryption Required.  Please go to <a href='" + TARGET + "'>" + TARGET + "</a> for this service.</body></html>")
                conn.sendline("")
    
                # kick em out
                conn.socket.close
    
    connections = ConnectionHandler()
    while 1:
        connections.run()

    Source code without formatting