Posts: 127
Joined: Thu Feb 14, 2013 2:10 am
Contact: Website

Urgent help needed! Where to put my Python CGI

Fri Feb 28, 2014 3:36 am

I have Apache2 web server running on my raspberry Pi and i have this camera app (http://www.raspberrypi.org/phpBB3/viewt ... 43&t=63276) installed too. but i'm not sure what web server is the app running. :(

I could host a simple web interface on localhost and i wanted to run a python file from my web interface so that I can control a servo by clicking the button.

I tried to use php exec function or jquery post method but all failed. so now i want to try CGI (a totally new term to me). According to this tutorial http://www.tutorialspoint.com/python/py ... amming.htm, I am suppose to find cgi-bin under /var/www but I found it in /usr/lib instead.

I tried to write a simple cgi as such http://raspberrywebserver.com/cgiscript ... ry-pi.html and put it in /usr/lib/cgi-bin. But when i go to this link localhost/cgi-bin/hello.cgi, there is nothing shown. So I wonder where should I put my cgi file? I wanted to write a python cgi instead of a bash cgi.

Or is there any alternative to call and run the python function from the webpage? I tried CherryPy before but the webpage itself is in the python script so i can just link the function... but I have no idea how to insert the camera app into my app that will be ran by cherrypy.

UPDATE: I have just successfully display a "hello world" at http://localhost/cgi-bin/hello.py However, now my question is, can i just run the python file without displaying anything?? As in without redirecting to a new page?
Just started my Raspberry Pi journey >> http://yvonnezoe.wordpress.com

User avatar
Posts: 39885
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Urgent help needed! Where to put my Python CGI

Fri Feb 28, 2014 5:53 pm

Your python program needs to send http headers even if there is no output.

You could send a response code 204 (instead of 200).

Code: Select all

print "'HTTP/1.0 204 NO CONTENT\r\n\r\n"
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Return to “Troubleshooting”