Refresh web page using a CGI Python script

自闭症网瘾萝莉.ら 提交于 2019-12-12 05:07:07

问题


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!


回答1:


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>")



回答2:


No, don't simply return the page - pressing e.g. 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.



来源:https://stackoverflow.com/questions/37399965/refresh-web-page-using-a-cgi-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!