How do I redirect a request to a different URL in Python
Asked Answered
H

5

30

I have been looking for the syntax to redirect a special URL to a remote server to do some XSS testing.

import SimpleHTTPServer
import SocketServer

class myHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        print self.path 
        if self.path == '/analog':
-------------------->return "http://12b.dk/analog"???
        return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

theport = 1234
Handler = myHandler
pywebserver = SocketServer.TCPServer(("", theport), Handler)

print "Python based web server. Serving at port", theport
pywebserver.serve_forever()
Hydantoin answered 24/3, 2010 at 10:40 Comment(0)
P
30

For a redirect, you have to return a code 301, plus a Location header. Probably you can try something like:

class myHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
   def do_GET(self):
       self.send_response(301)
       self.send_header('Location','http://www.example.com')
       self.end_headers()
Pains answered 24/3, 2010 at 10:57 Comment(0)
A
28

Python 3

In python3 it is done very similar to other answers, but different enough to justify demonstration.

This is a script that does nothing but listen on the port passed as argument 1 and send a 302 ("Found" aka Temporary) redirect to the URL passed as argument 2. (And it has a usage message.)

#!/usr/bin/env 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()

HTTPServer(("", int(sys.argv[1])), Redirect).serve_forever()

You call it like:

sudo ./redirect.py 80 http://jenkins.example.com:8080/

That example ought to give you enough to write what ever kind of function you need.

Antisepticize answered 2/11, 2017 at 20:39 Comment(1)
Answering questions in Python3, upvote for you, sir.Wheeler
K
9

This is a complete piece of code to redirect, save this file and run it as a python program. to terminate, ctrl + c.

import SimpleHTTPServer
import SocketServer
class myHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
   def do_GET(self):
       print self.path
       self.send_response(301)
       new_path = '%s%s'%('http://newserver.com', self.path)
       self.send_header('Location', new_path)
       self.end_headers()

PORT = 8000
handler = SocketServer.TCPServer(("", PORT), myHandler)
print ( "serving at port " + str(PORT) )
handler.serve_forever()
Keeling answered 4/3, 2014 at 20:8 Comment(0)
S
6

If you are trying to redirect other types of requests (eg. POST), you may need to use status code 307 instead of 301.

  • 301 will redirect with a GET request even if you sent a POST request
  • 307 will use the same method you used for the initial request

Example code that redirects GET and POST requests:

class myHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.redirect()
    def do_POST(self):
        self.redirect()
    def redirect(self):
        self.send_response(307)
        self.send_header('Location','http://www.example.com')
        self.end_headers()

Note: using status code 307 is potentially unsafe, so using this status code for all incoming requests is not recommended. Ideally, you would want to restrict its usage only for requests you know are safe.

Superstructure answered 30/5, 2018 at 15:1 Comment(1)
If you prefer, you can use HTTPStatus.TEMPORARY_REDIRECT instead of 307 all the values are found here: docs.python.org/3/library/http.html#http.HTTPStatusIdeational
A
1

HTTP status code 301 has meaning of the request moved permanently and should be redirected to suggested URI which is set to response header field, location. However, the redirection happens depending on implementation of browsers.

Instead of 301, returning 303 tells the browser the response for the request can be found under other URI and effectively manages the browser to redirects the GET request to another URI. Hense, 303 is a better option.

Anh answered 7/11, 2014 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.