Python CGI script returns "Exec format error"

Actually, the full error is:

Unable to launch the requested CGI program:
/www/cgi-bin/user_settings.py: Exec format error

The cgi script is:

#-*- coding: utf-8 -*-
import cgi

print "Content-type: text/html; charset=UTF-8"
print 

form = cgi.FieldStorage()

print "Hello"

The form element:

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

An ls -al shows:

root@ericyun:/www/cgi-bin# ls -al
-rwxr-xr-x    1 root     root           216 May  9 21:49 user_settings.py

And in the web server config, I added:

list interpreter        ".py=/usr/bin/python"

I've read through some of the other posts that ask about python CGI scripts and I think I got it all covered.

Thanks for any and all help. :slight_smile:

comment out

nano /etc/config/uhttpd
#list interpreter        ".py=/usr/bin/python"
/etc/init.d/uhttpd  restart
nano /www/cgi-bin/myfirst.py
#!/usr/bin/python
import cgi
import os
print "Content-type: text/html"
print
print """
<html>
<head><title>Sample CGI Script</title></head>
<body>
<h3> Sample CGI Script </h3>
"""
client = cgi.escape(os.environ["REMOTE_ADDR"])
print ('your IP Address is: ' + client + '
')
arguments = cgi.FieldStorage()
for key in arguments.keys():
  print key + '='
  print arguments[key].value  + '
'
print """
</body>
</html>
 """
chmod 755 /www/cgi-bin/myfirst.py

browser http://192.168.0.102/cgi-bin/myfirst.py?x=1&y=2

your IP Address is: 192.168.0.100
y= 2
x= 1

EricBrian:
::::SNIP::::
I've read through some of the other posts that ask about python CGI scripts and I think I got it all covered.

Thanks for any and all help. :slight_smile:

@EricBrian,

the first line should be code, blank, a blank comment, or have a shebang.

Google: unix shebang

So that means your first line should be:

#!/path/to/python

The line you have is to inform the interpreter that you will be using utf-8 source-code encoding.

PEP 0263 -- Defining Python Source Code Encodings

Lastly, the code by SonnyYu has a slight error. He forgot to include the first hash (#).

Jesse

jessemonroy650:
...
Lastly, the code by SonnyYu has a slight error. He forgot to include the first hash (#).
...

Thanks , typo is fixed.

I think sonny also has a typo in the nano command, I don't think the colon should be there.

Thank you @SonnyYu and @Jesse for this info. My script and the script by Sonny now produce this error:

The CGI process did not produce any response

I can get to the Arduino Web Panel without problems.

Thank you too @ShapeShifter. I use 'vi' anyhow and not nano so I knew what to do. :slight_smile:

EricBrian:
Thank you @SonnyYu and @Jesse for this info. My script and the script by Sonny now produce this error:

The CGI process did not produce any response

I can get to the Arduino Web Panel with no problems.

Thank you too @ShapeShifter. I use 'vi' anyhow and not nano so I knew what to do. :slight_smile:

This should help:

Web Server Configuration (uHTTPd)
http://wiki.openwrt.org/doc/uci/uhttpd

Happy Mom's Day
Jesse

jessemonroy650:
This should help:

Web Server Configuration (uHTTPd)
Web Server Configuration (uHTTPd) [Old OpenWrt Wiki]

Thank you for this Jesse. But I fail to see how that would help me. It appears that the web server works with the exception of my custom cgi scripts.

nano /www/cgi-bin/test.cgi
#!/usr/bin/python
print "Content-type: text/html"
print
print """
<html>
<head><title>Sample CGI Script</title></head>
<body>
<h3> Sample CGI Script </h3>
"""
print """
</body>
</html>
"""
chmod 755 /www/cgi-bin/test.cgi

Run it:

/www/cgi-bin/test.cgi
Content-type: text/html


<html>
<head><title>Sample CGI Script</title></head>
<body>
<h3> Sample CGI Script </h3>


</body>
</html>
/etc/init.d/uhttpd  restart

View it at http://192.168.0.102/cgi-bin/test.cgi

Thank you Sunny for the response.

The cgi script in this latest post from you worked great. However, your 1st script and my script still don't work. :frowning:

Add back missing code block one by one:

import cgi
import os
...
client = cgi.escape(os.environ["REMOTE_ADDR"])
print ('your IP Address is: ' + client + '
')

Test http://192.168.0.102/cgi-bin/test.cgi, next:

...
arguments = cgi.FieldStorage()
for key in arguments.keys():
  print key + '='
  print arguments[key].value  + '
'
...

browser http://192.168.0.102/cgi-bin/test.cgi?x=1&y=2

Sonny,

I reflashed the Yun and now things seem to work.

Thank you for your help! :wink:

EricBrian:
Thank you for this Jesse. But I fail to see how that would help me. It appears that the web server works with the exception of my custom cgi scripts.

@EricBrian,

reading the instructions is proven method of solving issues and bugs.

Jesse