简体   繁体   English

如何在 python 中将请求重定向到不同的 url

[英]How do I redirect a request to a different url in python

I have been looking for the syntax to redirect a special url to a remote server to do some XSS testing.我一直在寻找将特殊 url 重定向到远程服务器以进行一些 XSS 测试的语法。 Any ideas?有任何想法吗?

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()

For a redirect, you have to return a code 301, plus a Location header.对于重定向,您必须返回代码 301 和Location标头。 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()

Python 3蟒蛇 3

In python3 it is done very similar to other answers, but different enough to justify demonstration.python3它与其他答案非常相似,但差异足以证明演示是正确的。

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 .这是一个脚本,它只监听作为参数 1传递的端口,并将 302(“Found”又名临时)重定向到作为参数 2传递的 URL。 (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.该示例应该足以让您编写所需的任何类型的函数。

This is a complete piece of code to redirect, save this file and run it as a python program.这是一段完整的代码,用于重定向、保存此文件并将其作为 python 程序运行。 to terminate, ctrl + c.终止,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 8000"
handler.serve_forever()

If you are trying to redirect other types of requests (eg. POST), you may need to use status code 307 instead of 301 .如果您尝试重定向其他类型的请求(例如 POST),您可能需要使用状态代码307而不是301

  • 301 will redirect with a GET request even if you sent a POST request即使您发送了 POST 请求, 301也会使用 GET 请求重定向
  • 307 will use the same method you used for the initial request 307将使用您用于初始请求的相同方法

Example code that redirects GET and POST requests:重定向 GET 和 POST 请求的示例代码:

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.注意:使用状态代码307 可能不安全,因此不建议对所有传入请求使用此状态代码。 Ideally, you would want to restrict its usage only for requests you know are safe .理想情况下,您希望仅针对您知道安全的请求限制其使用。

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. HTTP 状态代码 301 具有永久移动请求的含义,应重定向到建议的 URI,该 URI 设置为响应标头字段、位置。 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.返回 303 而不是 301,返回 303 告诉浏览器可以在其他 URI 下找到请求的响应,并有效地管理浏览器将 GET 请求重定向到另一个 URI。 Hense, 303 is a better option. Hense,303是更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM