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?
I tried this fix i found an it doesn't work: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=82779
Hello World
" – 39fredy Oct 14 '15 at 02:45