简体   繁体   中英

Refresh web page using a CGI Python script

I have a very simple web page which I'm using in order to rotate a stepper motor on Raspberry Pi. My html file is this:

<html>
<head>
<script Language="Javascript">

function ccw()
{
  document.location="cgi-bin/rotate_45_ccw.py";
}

function cw()
{
  document.location="cgi-bin/rotate_45_cw.py";
}

</script>
</head>

<body>
  <div style="text-align:center">
    <h1>Raspberry Pi GPIO</h1>
<br>
  <button type="button", id="ccw", onclick="ccw()", value="value CCW">Rotate CCW</button>
  <button type="button", id="cw", onclick="cw()", value="value CW">Rotate CW</button>
<br>
  </div>

</body>
</html>

What I want to do is after either of the scripts is executed, for the page to be refreshed (so that the user can click again on either button). The dumb way I guess, is for the Python scripts to output the above html code. Is there a smarter/easier way?

Thanks!

After a LOT of searching around I finally got it right! So, in order to be redirected to the home page, I have to add the following print statements at the end of my python3 script:

print ("Content-type: text/html\n\n")
print ("<html><body>\n")
print ("<meta http-equiv=\"refresh\" content=\"0; url = http://192.168.1.109\" />")
print ("</body></html>")

No, don't simply return the page - pressing eg f5-reload will do the action again. Return a 303 See Other with the URL of the control page from the python script. So you always have the same "landing page" and don't have to code the same thing twice. Ideally the rotate actions should be http POSTs (they are not free of side-effects) but that is a different topic.

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