[英]How can one intercept an HTTP request, stop the request, and redirect the user to another site?
I am working on a url redirector application, in Python.我正在开发 url 重定向器应用程序,在 Python 中。 The idea of this application is simple: when a user performs an http request to a certain domain (eg to https://google.com ), the app stops the request and performs another http request (eg to https://github.com ), thereby redirecting the user to the second page.
The idea of this application is simple: when a user performs an http request to a certain domain (eg to https://google.com ), the app stops the request and performs another http request (eg to https://github. com ),从而将用户重定向到第二页。
Unfortunately, I have looked through SO and I haven't found any question that addresses this issue directly:不幸的是,我查看了 SO,但没有找到任何直接解决此问题的问题:
Admittedly, I only have some fabricated pseudocode to demonstrate what I wish to do, but it may prove useful:诚然,我只有一些伪造的伪代码来演示我想要做什么,但它可能会被证明是有用的:
import requests
original_site = "https://google.com"
redirect_site = "https://github.com"
def redirect_request():
if requests.get(original_site) == True:
requests.kill(request.original_site.id)
requests.get(redirect_site)
I greatly appreciate any suggestions.我非常感谢任何建议。
EDIT - Here is a clarification of what I mean:编辑 - 这是我的意思的澄清:
The user runs my python script, which I will call foobar.py
, as follows:用户运行我的 python 脚本,我将其称为
foobar.py
,如下所示:
python foobar.py
Then the user opens their web browser, and enters the url https://google.com , and as the script is running, the user will not actually visit https://google.com , but be redirected to https://github.com Then the user opens their web browser, and enters the url https://google.com , and as the script is running, the user will not actually visit https://google.com , but be redirected to https://github .com
One option is if you are trying to build a lightweight web app using python where you can mess with HTTP redirects, you can use Flask to accept a GET on a route and then simply do a redirect. One option is if you are trying to build a lightweight web app using python where you can mess with HTTP redirects, you can use Flask to accept a GET on a route and then simply do a redirect.
from flask import Flask, redirect
app = Flask(__name__)
@app.route('/')
def index():
return redirect("https://www.google.com")
But to answer your question more directly, you don't "stop" the initial request.但是要更直接地回答您的问题,您不会“停止”初始请求。 You simply find a way to handle it and serve back a response that you specify.
您只需找到一种方法来处理它并返回您指定的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.