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!

2 Answers 2

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>")
Sign up to request clarification or add additional context in comments.

2 Comments

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

4 Comments

Thanks Stefan! How exactly to return the 303 See Other? My CGI script currently returns print "Content-type: text/html\n\n".
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: <url of page>\n". Check e.g. stackoverflow.com/questions/17111809/… or stackoverflow.com/questions/6122957/…
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?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.