简体   繁体   English

执行CGI Python脚本后,如何自动返回主页?

[英]After a CGI Python script is executed, how can I return automatically to my homepage?

I have been playing around with a web server and calling a Python routine using the CGI module. 我一直在玩Web服务器,并使用CGI模块调用Python例程。 That was challenging but it works fine :) 那是具有挑战性的,但效果很好:)

My problem is after the Python script execution, I want to introduce a routine in my script to redirect again to my home page. 我的问题是执行Python脚本后,我想在脚本中引入一个例程以再次重定向到我的主页。

I have seen in this forum questions like this one and the answer in most cases is: use the command line print("Location:http://your_page") , but I have already trie it and does not work in my case. 我在这个论坛上看到过类似这样的问题,大多数情况下的答案是:使用命令行print("Location:http://your_page") ,但是我已经尝试过了,在我的情况下不起作用。

In the next paragraph I will try to explain my whole "mini project" and then I let you see my code. 在下一段中,我将尝试解释我的整个“迷你项目”,然后让您看一下我的代码。

Remote control of wireless home system through internet. 通过互联网对无线家庭系统进行远程控制。 Language Python and PHP, also Linux knowledge OS 语言Python和PHP,还有Linux知识操作系统

  • The purpose of this project is clicking a button on a web page (internet connection) turn on and off a LED. 该项目的目的是单击网页上的一个按钮(互联网连接)以打开和关闭LED。 The LED is in a wireless module somewhere at home. LED位于家里某个地方的无线模块中。

  • Setting an Apache2 web server running on BeagleBone Black (Debian OS). 设置在BeagleBone Black(Debian OS)上运行的Apache2 Web服务器。 Beagle Bone Black is connected via UTP cable to LAN. Beagle Bone Black通过UTP电缆连接到LAN。

  • Programming a ESP32 module as TCP server and also connected to Wi-Fi (LAN). 将ESP32模块编程为TCP服务器并连接到Wi-Fi(LAN)。

  • Programmed Python script as CGI module to execute a TCP client connection with ESP32 module and commanding turn of and then turn off a LED. 将Python脚本编程为CGI模块,以执行与ESP32模块的TCP客户端连接,并命令关闭然后关闭LED。 The script is executed when a button is clicked in the web page. 当在网页中单击按钮时,将执行脚本。

  • Setting a port forward on Modem/Router to allow access from internet to the Web Server. 在调制解调器/路由器上设置端口转发,以允许从Internet访问Web服务器。

index.html (Home page) index.html(主页)

<!DOCTYPE html>
<html>
  <head>
    <title>Web Server MReyesP!</title>
    <style>
      .button {
        padding: 15px 25px;
        font-size: 24px;
        text-align: center;
        cursor: pointer;
        outline: none;
        color: #fff;
        background-color: #4CAF50;
        border: none;
        border-radius: 15px;
        box-shadow: 0 9px #999;
      }
      .button:hover {background-color: #3e8e41}
      .button:active {
        background-color: #3e8e41;
        box-shadow: 0 5px #666;
        transform: translateY(4px);
      }
    </style>
  </head>
  <body>
    <h1>Success! The Web Server is working :)</h1>
    <h2>Click the Button to run - "LED On/Off program"</h2>
    <input class=button onClick="location.href='http://my_web_page.net/cgi-bin/TCPC$
  </body>
</html>

This is my resulting home page. 这是我的主页。 Home page 主页

python_script.py python_script.py

#!/usr/bin/python3

import socket
import time
import cgitb
cgitb.enable()

print("Content-type: text/html\n\n")
print("<html>\n<body>")
print("<div style=\"with:100%; font-size: 40px; font-weight bold; text-align: c$
print("The LED was turned On and after 2 seconds was turned Off")
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip="192.168.2.112"
port=1234
address=(ip,port)
client.connect(address)
data = 'led_on'
client.sendall(data.encode('utf-8'))
time.sleep(2)
data = 'led_off'
client.sendall(data.encode('utf-8'))
client.close()
print("Location: http://my_web_page.net/\n")
print("</div>\n</body>\n</html>")

As result my Python script executes the routine but does not redirect back to my home page. 结果,我的Python脚本执行了例程,但没有重定向回到我的主页。 You can see by yourself the result in the next image. 您可以在下一张图像中自己查看结果。 This web page stays forever. 该网页永远存在。

最终网页

Do you have any ideas? 你有什么想法? What do I have to insert in my Python code to come back to my home page? 我必须在Python代码中插入什么才能返回首页?

Thanks guys, I really appreciate your time and help. 谢谢大家,我非常感谢您的宝贵时间和帮助。

Note: I am using Python 3. 注意:我正在使用Python 3。

If you want to print your own output and redirect, you'll need to emit a snippet of simple javascript in your response to do the redirect. 如果要打印自己的输出重定向,则需要在响应中发出一个简单的javascript代码段来进行重定向。

If you don't need output from your CGI at all, you need to print the Location: header before you print the double \\n\\n that terminates the headers. 如果根本不需要CGI的输出,则需要先打印Location:标头,然后再打印终止标头的双\\ n \\ n。 You also need to add the "pseudo" header Status: to set a 301 or 302 response code. 您还需要添加“伪”标头Status:来设置301或302响应代码。

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

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