Python CGI Script: OSError: [Errno 8] Exec format error

cgi, python

Solution

The solution in this case was that I was using the wrong version of Python to run the script. The default in osx 10.9 is Python 2.7.5. I wanted to run it with python 3.3, therefore my solution was simply to execute it with the appropriate version: `#!/usr/local/bin/python3`

Problem

When I send `http://localhost:8888/cgi-bin/peoplecgi.py?action=Fetch&key=sue` (sue is a valid key in the shelve) to the cgi script below I get the following (also I'm on OSX with Python 3.3). Any ideas what is going wrong? ``` 127.0.0.1 - - [04/Feb/2014 10:38:41] "GET /cgi-bin/peoplecgi.py?action=Fetch&key=sue HTTP/1.1" 200 - Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/server.py", line 1131, in run_cgi os.execve(scriptfile, args, env) OSError: [Errno 8] Exec format error: '/Users/rich/Google Drive/Code/Python/PP4E/Preview/cgi-bin/peoplecgi.py' 127.0.0.1 - - [04/Feb/2014 10:38:41] CGI script exit status 0x7f00 ``` I am working out of O'Reilly's Programming Python 4th Edition. This is question is based off example 1-33. weberver.py: ``` import os, sys from http.server import HTTPServer, CGIHTTPRequestHandler webdir = '/Users/rich/Google Drive/Code/Python/PP4E/Preview/' port = 8888 os.chdir(webdir) srvraddr = ("", port) srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler) srvrobj.serve_forever() ``` peoplecgi.py ``` import cgi, shelve, sys, os shelvename = 'class-shelve' fieldnames = ('name', 'age', 'job', 'pay') form = cgi.FieldStorage() print('Content-type: text/html') sys.path.insert(0, os.getcwd()) replyhtml=""" <html> <title>People Input Form</title> <body> <form method=POST action="peoplecgi.py"> <table> <tr><th>key<td><input type=text name=key value="%(key)s"> $ROWS$ </table> <p> <input type=submit value="Fetch", name=action> <input type=submit value="Update", name=action> </form> </body></html> """ rowhtml = '<tr><th>%s<td><input type=text name=%s value="%%(%s)s">\n' rowshtml = '' for fieldname in fieldnames: rowshtml += (rowhtml % ((fieldname,) * 3)) replyhtml = replyhtml.replace('$ROWS$', rowshtml) def htmlize(adict): new = adict.copy() for field in fieldnames: value = new[field] new[field] = cgi.escape(repr(value)) return new def fetchRecord(db, form): try: key = form['key'].value record = db[key] fields = record.__dict__ fields['key'] = key except: fields = dict.fromkeys(fieldnames, '?') fields['key'] = 'Missing or invalid key!' return fields def updateRecord(db, form): if not 'key' in form: fields = dict.fromkeys(fieldnames, '?') fields['key'] = 'Missing key input!' else: key = form['key'].value if key in db: record = db[key] else: from person import Person record = Persion(name='?', age='?') for field in fieldnames: setattr(record, field, eval(form[field].value)) db[key] = record fields = record.__dict__ fields['key'] = key return fields db = shelve.open(shelvename) action = form['action'].value if 'action' in form else None if action == 'Fetch': fields = updateRecord(db, form) else: fields = dict.fromkeys(fieldnames, '?') fields['key'] = 'Missing or invalid action!' db.close() print(replyhtml % htmlize(fields)) ```

Original source