puggan icon

rickroll_http.py

puggan | PRO | 10/03/18 10:36:17 PM UTC | 0 ⭐ | 1076 👁️ | Never ⏰ | []
Python |

1.88 KB

|

None

|

0 👍

/

0 👎

import time
import BaseHTTPServer
PORT_NUMBER = 114
URL = 'https://www.latlmes.com/music/http-is-insecure-1'
URL_IMAGE = 'https://www.dailydot.com/wp-content/uploads/e0f/b5/794db6eb765355fe14eda1d24c314b18.jpg'
 
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
    def do_HEAD(s):
        s.redirect()
        return
 
    def do_GET(s):
        s.guess_type()
        return
 
    def do_POST(s):
        s.guess_type()
        return
 
    def guess_type(s):
        if s.path.endswith('.js'):
            s.handle_js()
            return
        if s.path.endswith('.png'):
            s.redirect_image()
            return
        if s.path.endswith('.gif'):
            s.redirect_image()
            return
        if s.path.endswith('.jpeg'):
            s.redirect_image()
            return
        if s.path.endswith('.jpg'):
            s.redirect_image()
            return
        if s.path.endswith('.svg'):
            s.redirect_image()
            return
        s.redirect()
        return
 
    def redirect(s):
        s.send_response(301)
        s.send_header("Location", URL)
        s.end_headers()
        return
 
    def redirect_image(s):
        s.send_response(301)
        s.send_header("Location", URL_IMAGE)
        s.end_headers()
        return
 
    def handle_js(s):
        s.send_response(200)
        s.send_header("Content-type", "text/javascript")
        s.end_headers()
        s.wfile.write("window.location.href = '" + URL + "';\n")
        return
 
if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class(('', PORT_NUMBER), MyHandler)
    print time.asctime(), "Server Starts - %s" % (PORT_NUMBER)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    print time.asctime(), "Server Stops - %s" % (PORT_NUMBER)

Comments