简体   繁体   English

如何拦截 HTTP 请求、停止请求并将用户重定向到另一个站点?

[英]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.

相关问题 如何从我的 FastAPI 应用程序向另一个站点 (API) 发送 HTTP 请求? - How can I send an HTTP request from my FastAPI app to another site (API)? 拦截请求,然后转发到另一台服务器 - Intercept request and then forward to another Server 如何在Django中忽略或重定向http请求? - How to ignore or redirect a http request in Django? Python-从页面拦截HTTP请求 - Python - Intercept HTTP request from a page 在 Django 中的 http 请求中使用不同参数多次重定向到一个站点 - Multiple redirects to one site with different parameters in http request in Django 如何根据用户请求停止嗅探 - How to stop sniffing upon user request 如何使用请求上下文重定向到Django中的另一个URL - How to redirect with request context to another url in Django 如果该用户是一个站点的超级用户,而另一个站点是活动的,该如何重定向用户? 登录后找不到页面(404) - How to redirect a user if it is superuser one site and if another is active? After login Page not found (404) 我可以将http请求从一个node.js或python服务器转发到另一台服务器吗? - Can I forward an http request from one node.js or python server to another server? 在 Django 中向 admin:auth_user_password_change 发送 POST 请求后如何重定向到另一个 URL? - How to redirect to another url after sending a POST request to admin:auth_user_password_change in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM