vectraboyv6
Posts: 43
Joined: Fri Aug 17, 2012 11:28 am

running a program for a web page

Fri Nov 22, 2013 12:00 pm

guys,

I have a C++ program obtained from the internet to control 433mhz remote power plugs. I have captured the codes from my remote change the file and compiled it and it all works nicely from the command prompt via ssh.

simply $ ./switch 1 on etc.....

I am also using webiopi to view some db18d20s temp probes

please does any one know how I can use my "switch" program via the web page. I been trying to work this out for like 5 days now and by god just why cant it be simple! I've tried Perl but no joy also python and php and cgi's but I must be so thick nothing wants to work.....

please help as im on my last ounce of patience and its heading for the bin on Sunday if still no joy

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

Re: running a program for a web page

Fri Nov 22, 2013 12:43 pm

You need to drive it from the Common Gateway Interface (CGI) in a web server. CGI isn't difficult the web server sends GET or POST requested to stdin, your CGI program emits HTML on stdout. The main thing is the restricted enviroment where your program runs.

Perl has a simple module CGI (https://www.google.co.uk/search?q=perl+cgi) which takes the sting out of CGI programming. Just ask if you'd like a simple Perl CGI sample.

It's usually sensible to put a demarcation layer between your program (which possibly needs root privileges if it's working with GPIO) and the CGI program which runs as user www-data in the protected CGI environment. That means you're less likely to compromise security when the web hackers arrive on your website. It also means you can separate the CGI layer from the server layer (even by machine if you want).

I wrote a sample with a python CGI program that sends a message to MQTT to get my python server program to set a tri-colour LED. https://www.ibm.com/developerworks/comm ... x_and_gpio

MQTT is a neat way to send messages from a publisher to one (or many) subscribers and has a nice clean interface (no socket programming needed). http://mqtt.org. The MQTT server I'm running is Mosquitto http://mosquitto.org
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 non-medical doctors are on my foes list.

vectraboyv6
Posts: 43
Joined: Fri Aug 17, 2012 11:28 am

Re: running a program for a web page

Fri Nov 22, 2013 1:31 pm

thanks for the response,

ive tried some simple test .cgi scripts within my python web server but it refuses to run them.. the only thing it ran was a simple perl script.

the webiopi software has its own webserver built in but I don't know if this can run cgi's etc.....

im going to reformat my sd card and start again as ive been through so may settings I may of opps something along that way.

could you upload a script that would say list the contents of the directory via a web page for me to understand?

thanks for your help

User avatar
emgi
Posts: 357
Joined: Thu Nov 07, 2013 4:08 pm
Location: NL

Re: running a program for a web page

Fri Nov 22, 2013 1:41 pm

This is not rocket science. As the previous poster already pointed out, your CGI runs in a protected environment to secure the system against unauthorized use. One of the things it cannot do by default is start a shell or run shell commands. Writing to files outside the document_root is another. Fortunately there are ways around this but you must beware not to open up your box too far.

With Perl I managed to write scripts which start a shell script, update counters, write to a database and create .png files.
Important points:
-Check the file access rights
-Preferably run in the document_root /cgi-bin or a similar directory you created.
-In your scripts, always use the full path.

Here is an example website I made in Perl for someone else here on the forum.
It can run java scripts and checks a file to see if a switch is actually on or off.
http://cable-79-207.zeelandnet.nl/RCMS-Pi.tar

It runs on the lighttpd webserver, my favorite as it has always done what I wanted without any issues.

/emgi

Return to “Beginners”