1

I want to be able to run a python script when I press a button in an HTML webpage. I have followed this example and it works. But when I change the python code to my own I get the following error in the apache/log/error.log:

[Wed Oct 14 01:14:11 2015] [error] (8)Exec format error: exec of '/var/www/cgi-bin/ha.py' failed [Wed Oct 14 01:14:11 2015] [error] [client 18.111.23.211] Premature end of script headers: ha.py, referer: http://18.111.31.172/page1.html

I followed the tutorial: http://xmodulo.com/create-use-python-cgi-scripts.html Here is the code for both page1.html and ha.py.

#!/usr/bin/python

from xbee import XBee,ZigBee
import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
list_a=    [b"\x00\x13\xA2\x00\x40\xC7\x36\x70",b"\x00\x13\xA2\x00\x40\xC7\x36\x76",b"\x00\x13\xA2\x00\x40\xE8\x05\xCF"]
address64 = b"\x00\x13\xA2\x00\x40\xEC\x3B\xEA"
payload = b"\x03"
# Use an XBee 802.15.4 device
# To use with an XBee ZigBee device, replace with:
zb = ZigBee(ser)
#xbee = XBee(ser)

# Set remote DIO pin 2 to low (mode 4)
for i in range(len(list_a)):

print("sending data")
zb.send("tx",dest_addr_long=list_a[i],dest_addr=b"\xFF\xFE",data=payload)


ser.close()

and HTML page1.html

<html>
<h1>Test Page 1</h1>
<form name="input" action="/cgi-bin/ha.py" method="post">
<input type="submit" value="Submit">
</form>
</html>

I changed the html method to "post" from get to see if that would do anything. All I want to do is run the python code and not change anything to in the html page.

Any help would be appreciated! Thanks!

Updated python code:

#!/usr/bin/python

from xbee import XBee,ZigBee
import serial
print 'Status: 205 No Response'
print ""
ser = serial.Serial('/dev/ttyAMA0', 9600)
list_a=[b"\x00\x13\xA2\x00\x40\xC7\x36\x70",b"\x00\x13\xA2\x00\x40\xC7\x36\x76",b"\x00\x13\xA2\x00\x40\xE8\x05\xCF"]
address64 = b"\x00\x13\xA2\x00\x40\xEC\x3B\xEA"
payload = b"\x03"
# Use an XBee 802.15.4 device
# To use with an XBee ZigBee device, replace with:
zb = ZigBee(ser)
#xbee = XBee(ser)

# Set remote DIO pin 2 to low (mode 4)
for i in range(len(list_a)):

#print("sending data")
    zb.send("tx",dest_addr_long=list_a[i],dest_addr=b"\xFF\xFE",data=payload)


ser.close()

update 2: new ha.py is:

#!/usr/bin/python

import cgi
import cgitb
from xbee import XBee,ZigBee
import serial

cgitb.enable()
print 'Status: 205 No Response'
print ""

Most current python script: #!/usr/bin/python

import cgi
import cgitb
from xbee import XBee,ZigBee
import serial

cgitb.enable()
#print 'Status: 205 No Response'
#print ""
print "Content-type: text/html\n\n"
print "<h1>Hello World</h1>"

ser = serial.Serial('/dev/ttyAMA0', 9600)
list_a=[b"\x00\x13\xA2\x00\x40\xC7\x36\x70",b"\x00\x13\xA2\x00\x40\xC7\x36\x76",b"\x00\x13\xA2\x00\x40\xE8\x05\xCF"]
address64 = b"\x00\x13\xA2\x00\x40\xEC\x3B\xEA"
payload = b"\x03"
# Use an XBee 802.15.4 device
# To use with an XBee ZigBee device, replace with:
zb = ZigBee(ser)
#xbee = XBee(ser)

# Set remote DIO pin 2 to low (mode 4)
for i in range(len(list_a)):

#print("sending data")
    zb.send("tx",dest_addr_long=list_a[i],dest_addr=b"\xFF\xFE",data=payload)


ser.close()

and I get this new error: (see image) Any help?enter image description here

I tried this fix i found an it doesn't work: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=82779

39fredy
  • 1,923
  • 2
  • 21
  • 40
  • Where are you outputting the headers? – Ignacio Vazquez-Abrams Oct 14 '15 at 01:41
  • What do you mean? What headers ? – 39fredy Oct 14 '15 at 01:44
  • The headers that you need to output for every CGI script ever. – Ignacio Vazquez-Abrams Oct 14 '15 at 01:44
  • I followed the tutorial from: http://xmodulo.com/create-use-python-cgi-scripts.html I didn't know that you have to output headers. Can you help me out? – 39fredy Oct 14 '15 at 01:47
  • Start reading from "The contents of /var/www/cgi-bin/myscript-1.py are:". – Ignacio Vazquez-Abrams Oct 14 '15 at 01:49
  • Oh I see thank you. " The print "Content-Type: text/html" statement is required so that the web server knows what type of output it is receiving from the CGI script" The issue is that i don't want to output anything. I juts want the python script to run. – 39fredy Oct 14 '15 at 01:52
  • Then a) you need to not print anything, and b) you need to tell the browser to not expect anything via the correct status code. – Ignacio Vazquez-Abrams Oct 14 '15 at 01:53
  • So not print anything in the python code? How exactly do I do b? I've been reading this http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html and I don't quite understand it. – 39fredy Oct 14 '15 at 01:55
  • http://stackoverflow.com/questions/1411867/python-cgi-returning-an-http-status-code-such-as-403 – Ignacio Vazquez-Abrams Oct 14 '15 at 01:57
  • Would I then send: print 'Status: 204 No Response'? For the method would I then change that to 'get'? thanks! – 39fredy Oct 14 '15 at 02:03
  • The GET method results in a change in the URL shown in the browser. Also, you should consider if 205 might be more appropriate. And don't forget the blank line. – Ignacio Vazquez-Abrams Oct 14 '15 at 02:05
  • I've had done the following changes and I still get no results. Any other tips? Still get the following error: [Wed Oct 14 02:11:44 2015] [error] (8)Exec format error: exec of '/var/www/cgi-bin/ha.py' failed [Wed Oct 14 02:11:44 2015] [error] [client 18.111.23.211] Premature end of script headers: ha.py, referer: http://18.111.31.172/page1.html – 39fredy Oct 14 '15 at 02:12
  • Did you forget the blank line? – Ignacio Vazquez-Abrams Oct 14 '15 at 02:12
  • Then one of your imports is probably failing. See the `cgitb` module documentation. – Ignacio Vazquez-Abrams Oct 14 '15 at 02:19
  • But in python I'm using "import cgi" not "import cgitb" does that matter ? – 39fredy Oct 14 '15 at 02:23
  • Im currently reading this: http://raspberrywebserver.com/cgiscripting/writing-cgi-scripts-in-python.htmlm the debugging python code – 39fredy Oct 14 '15 at 02:26
  • How exactly does cgitb work? should i then print a line like: print "Content-Type: text/plain" – 39fredy Oct 14 '15 at 02:38
  • I have also switch the python script and it seems to work: the new python script has the following: #!/usr/bin/python import cgi import cgitb cgitb.enable() print "Content-type: text/html\n\n" print "

    Hello World

    "
    – 39fredy Oct 14 '15 at 02:45
  • I think it is an issue with not sending anything and having the "get" in the method. I have changed the script to the following( see above under update 2) – 39fredy Oct 14 '15 at 02:49

0 Answers0