简体   繁体   中英

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. 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.

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.

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

  • The purpose of this project is clicking a button on a web page (internet connection) turn on and off a LED. The LED is in a wireless module somewhere at home.

  • Setting an Apache2 web server running on BeagleBone Black (Debian OS). Beagle Bone Black is connected via UTP cable to LAN.

  • Programming a ESP32 module as TCP server and also connected to 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. 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.

index.html (Home page)

<!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

#!/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. 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?

Thanks guys, I really appreciate your time and help.

Note: I am using 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.

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. You also need to add the "pseudo" header Status: to set a 301 or 302 response code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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