1

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!

panos
  • 41
  • 2
  • 9

2 Answers2

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>")
panos
  • 41
  • 2
  • 9
  • Good - I thought it could have been done at the http Level itself without involving the html meta tag - but surely that will work for browsers (but maybe not if you use other tools like cURL to pull the page) – Stefan Hegny Jun 03 '16 at 06:32
  • Yes, I understand that this is different than sending a HTTP status code, even though I don't really understand how it works :p... The thing is, however, that it was **very** difficult to find documentation on how to do this using status codes. I'm guessing it's so simple that nobody bothers to explain it for noobs like myself ;) – panos Jun 04 '16 at 08:17
0

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.

Stefan Hegny
  • 2,107
  • 4
  • 23
  • 26
  • Thanks Stefan! How exactly to return the `303 See Other`? My CGI script currently returns `print "Content-type: text/html\n\n"`. – panos May 23 '16 at 21:04
  • 1
    hmm, not so good at python cgi scripts myself. Normally this would be above the Content-type stuff as like `"HTTP/1.1 303 See Other\nLocation: \n"`. Check e.g. http://stackoverflow.com/questions/17111809/redirection-from-a-python-cgi-page-to-another-page-without-hyperlinks or http://stackoverflow.com/questions/6122957/webpage-redirect-to-the-main-page-with-cgi-python – Stefan Hegny May 24 '16 at 06:21
  • Thanks for the additional info. When I print only those two lines (`"HTTP/1.1..."` and `"Location:...."`) I get an error ("Bad header"). So, should those two lines be the ONLY output of my script? Should there be something else? – panos May 24 '16 at 17:39
  • Well, followed by either more response headers or `\r\n\r\n` .(\r\n DOS-style newlines are ok anywhere between the header lines) I'm sorry I can't help you there technically; you might check usual sources of documentation or search here or try to make a dedicated question out of it. The problem cannot be so obscure, several others should have done it - good luck – Stefan Hegny May 24 '16 at 20:39