title: Running cgi ( Python scripts) in HTML Raspberry Pi using apache
body: <p>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:</p>
  
  <p>[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: <a href="http://18.111.31.172/page1.html" rel="nofollow noreferrer">http://18.111.31.172/page1.html</a></p>
  
  <p>I followed the tutorial: <a href="http://xmodulo.com/create-use-python-cgi-scripts.html" rel="nofollow noreferrer">http://xmodulo.com/create-use-python-cgi-scripts.html</a>
  Here is the code for both page1.html and ha.py.</p>
  
  <pre><code>#!/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()
  </code></pre>
  
  <p>and HTML page1.html</p>
  
  <pre><code>&lt;html&gt;
  &lt;h1&gt;Test Page 1&lt;/h1&gt;
  &lt;form name="input" action="/cgi-bin/ha.py" method="post"&gt;
  &lt;input type="submit" value="Submit"&gt;
  &lt;/form&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>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. </p>
  
  <p>Any help would be appreciated! Thanks!</p>
  
  <p>Updated python code:</p>
  
  <pre><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()
  </code></pre>
  
  <p>update 2: new ha.py is:</p>
  
  <pre><code>#!/usr/bin/python
  
  import cgi
  import cgitb
  from xbee import XBee,ZigBee
  import serial
  
  cgitb.enable()
  print 'Status: 205 No Response'
  print ""
  </code></pre>
  
  <p>Most current python script: 
      #!/usr/bin/python</p>
  
  <pre><code>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 "&lt;h1&gt;Hello World&lt;/h1&gt;"
  
  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()
  </code></pre>
  
  <p>and I get this new error: (see image)
  Any help?<a href="https://i.stack.imgur.com/5eyXv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5eyXv.png" alt="enter image description here"></a></p>
  
  <p>I tried this fix i found an it doesn't work:
  <a href="https://www.raspberrypi.org/forums/viewtopic.php?f=28&amp;t=82779" rel="nofollow noreferrer">https://www.raspberrypi.org/forums/viewtopic.php?f=28&amp;t=82779</a></p>