mikemitza
Posts: 11
Joined: Thu Oct 11, 2012 4:32 pm

CGI script calling a simple Python script problem

Fri Jun 14, 2013 3:33 pm

Hello. I am working on a project that involves using a webpage to enter a numerical value and passing that value to the RasPi and using it in a digital-analog conversion to output a voltage. I think I got the code for the cgi right but the DAC doesn't return any voltage.
The webpage code is:

Code: Select all

<html>
....
<head>
<script language="javascript" type="text/javascript">
function showValue(newValue)
{
	document.getElementById("range").innerHTML=newValue;
}
</script>

</head>

<body>
...
<form action="/cgi-bin/control.py" method="POST">

<div id="myDiv" name="myDiv" title="Example Div Element" style="color: #0900C4; font: Helvetica 12pt;border: 1px solid black;">

<h4 style="text-align:center;">Control parametrii: </h4>

<input id="param1" name="param1" type="range"  min="0" max="4095" value="0" step="1" onchange="showValue(this.value)" />

<span id="range">0</span>

<div><input type="submit" value="Trimite valoarea"></div>

</div>

</form>

</body>

</html>
and the cgi code is:

Code: Select all

#!/usr/bin/python
import os
import subprocess
import cgi;
import cgitb
cgitb.enable()


def print_header():
	print """Content-type: text/html\n
	<!DOCTYPE html>
	<html>
	<body>"""

def print_close():
	print """</body>
	</html>"""

def display_data(valoare):
	print_header()
	print "<p>Valoarea " + valoare + " a fost transmisa cu succes.</p>"
	print_close()

def display_error():
	print_header()
	print "<p>An Error occurred parsing the parameters passed to this script.</p>"
	print "<p>Try something like:</p>"
	print "<p><strong>http://localhost/cgi.py?param1=1</strong></p>"
	print_close()

def main():
	form = cgi.FieldStorage()
	if (form.has_key("param1")):
		val=form["param1"].value
		display_data(form["param1"].value)
		os.system("python /var/www/cgi-bin/dac.py 'val'")
		
	else:
		display_error()

main()
When submitted, the text "Valoarea .... a fost transmisa cu succes." is returned but the converter doesn't convert the number. The cgi script passes the value val to the dac script, or at least it should. I am using this method for calling the other script to avoid /dev/mem errors. Does anyone have any suggestions? Thank you.

Mihai
Last edited by mikemitza on Fri Jun 14, 2013 9:02 pm, edited 1 time in total.

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: CGI script calling a simple Python script problem

Fri Jun 14, 2013 7:14 pm

first, try editing your post by removing your code and re-pasting the code from your program inside of the code tags, it makes it much easier to read and helps rule out alot of the issues that most people have with tab spacing in thier programs and diagnosing issues like that.

Second, just looking at it by glance, I see the "valoare" varable trying to print out to the screen, but there's no other places that the varable is used, all of the other varables for being called back on is var. Also, I believe that the

Code: Select all

os.system("python /var/www/cgi-bin/dac.py 'val'")
line should be something like

Code: Select all

valoare = os.system("python /var/www/cgi-bin/dac.py 'val'")
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

mikemitza
Posts: 11
Joined: Thu Oct 11, 2012 4:32 pm

Re: CGI script calling a simple Python script problem

Fri Jun 14, 2013 9:08 pm

Thank you for your reply and for your remarks. I think I didn't explain correctly what I want to do :)
I want the cgi script to get the value from the field named param1 in the html doc and to pass it to the script dac.py. The variable named "valoare" is a local variable from the function display_data(). It is used in the main() function with the value gotten from the html form, saved in the variable "val". The dac script called by the os.system function must get the "val" value and use it in the conversion to voltage.

mikemitza
Posts: 11
Joined: Thu Oct 11, 2012 4:32 pm

Re: CGI script calling a simple Python script problem

Sat Jun 15, 2013 7:40 am

Can anyone tell me if I passed the variable 'val' correctly?
I used the info found on this page: http://www.linuxquestions.org/questions ... es-572485/

ghp
Posts: 1597
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: CGI script calling a simple Python script problem

Sat Jun 15, 2013 8:22 am

Hello,

the code

Code: Select all

os.system("python /var/www/cgi-bin/dac.py 'val'")
will put a string 'val' to the command line of the dac.py-code.
Try

Code: Select all

os.system("python /var/www/cgi-bin/dac.py '{:d}'".format(val))
. This will place the value of val as a string into the command line.
Check this by placing a print-statement into the dac.py-code and printout the incoming command line parameter.

Debugging cgi-scripts can be tricky, as they run far away of any debugger or handy printout.
First, check the apache log files for errors.
Second, use the system logger, which places data to the /var/log/messages file.
Example:
Command line, use 'logger hello'. With 'tail -f /var/log/messages', you see this message.
For your cgi-script, you can use this technique by adding a line close to the os.system.command:

Code: Select all

os.system("logger  /var/www/cgi-bin/dac.py 'val'")
. This will write the commandline to /var/log/messages, and you do not need to tweak the dac.py. In addition to this, it will keep some history on the usage of the web page.

Gerhard

User avatar
rpdom
Posts: 17721
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: CGI script calling a simple Python script problem

Sat Jun 15, 2013 8:39 am

I don't know python, but what you seem to be doing is passing the word "val" to the command instead of the value of the variable "val".

I think it should be something like

Code: Select all

os.system("python /var/www/cgi-bin/dac.py '" + val + "'")
Watch out for all the single and double quotes there!

mikemitza
Posts: 11
Joined: Thu Oct 11, 2012 4:32 pm

Re: CGI script calling a simple Python script problem

Sat Jun 15, 2013 3:38 pm

Hello and thank you all for your answers. I tried all your advises but none seem to work. I think there is some problem with the file permissions and owners.
The files have the following owners and permissions:

Code: Select all

-rwxr-xr-x   pi   www-data  control.py
-rwxr-xr-x   pi   pi  dac.py
www-data is a group created for the project.
Can you tell me if this is ok?
Thank you all.
Mihai

mikemitza
Posts: 11
Joined: Thu Oct 11, 2012 4:32 pm

Re: CGI script calling a simple Python script problem

Sun Jun 16, 2013 9:40 am

Hello again. Now I'm getting /dev/mem errors from my script. The script dac.py uses the RPi.GPIO library and I've read that you can't access the GPIOs with it from a webpage because you need to be root and that's why I am now getting the /dev/mem error, because it's a measure of protecting the kernel. Does anyone know how to get root privileges for this project. I don't mind the security gap, it's a home project :) Also, I've read that you can use WiringPi to handle this problem but I would like to keep the original script. Thank you.

Mihai

mikemitza
Posts: 11
Joined: Thu Oct 11, 2012 4:32 pm

Re: CGI script calling a simple Python script problem

Mon Jun 24, 2013 9:44 pm

Hello. I think I managed to get the permissions right but I have a new error:

Code: Select all

sudo: no tty present and no askpass program specified
I found this in the apache log. Does anyone know any solutions?


akosipolan
Posts: 13
Joined: Sat Jul 19, 2014 7:16 am

Re: CGI script calling a simple Python script problem

Tue Aug 11, 2015 9:22 am

mikemitza wrote:Hello again. Now I'm getting /dev/mem errors from my script. The script dac.py uses the RPi.GPIO library and I've read that you can't access the GPIOs with it from a webpage because you need to be root and that's why I am now getting the /dev/mem error, because it's a measure of protecting the kernel. Does anyone know how to get root privileges for this project. I don't mind the security gap, it's a home project :) Also, I've read that you can use WiringPi to handle this problem but I would like to keep the original script. Thank you.

Mihai
Good Day Sir, how did you set the permission of the file to be able to use and be able to call the os.system on your python cgi?

Edit: nevermind I got it.

Return to “Python”