Summary: Python cgi script runs as expected when called from a simple python debug server, but fails with
500 Premature end of script headers
error when run from~/public_html/cgi-bin/
My CGI script works fine when run through a simple python webserver, and I see the right output when navigating to nameofmyhost.com:8080/...
However, when running the same script from my public_html/cgi-bin
directory it gives me a 500 premature end of script headers
error. What can I do to fix this?
The permissions on the file seem ok:
drwxrwxrwx cgi-bin
-rwxr-xr-x cgi-bin/generate_list.py
This is simple_httpd.py, the simple python webserver
from http.server import HTTPServer, CGIHTTPRequestHandler
port = 8080
httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
httpd.serve_forever()
CGI script generate_list.py:
#! /usr/local/bin/python3
import athletemodel
import yate
import glob
data_files = glob.glob("data/*.txt")
athletes = athletemodel.put_to_store(data_files)
print(yate.start_response())
print(yate.include_header("Coach Kelly's List of Athletes"))
print(yate.start_form("generate_timing_data.py"))
print(yate.para("Select an athlete from the list to work with:"))
for each_athlete in athletes:
print(yate.radio_button("which_athlete", athletes[each_athlete].name))
print (yate.end_form("Select"))
print(yate.include_footer({"Home": "/index.html"}))
I'm guessing I need to maybe explicitly state my directory somewhere, maybe?
PS: I am going through the Head First Python book by Oreilly
Check that the server can locate my files
Server finds simple html file in public_home [OK]
Check that the CGI script can execute without error.
Content-type: text/html <html> <head> <title>Coach Kelly's List of Athletes</title> [...]
CGI script ran from command line - outputs as expected [OK]
Check that the server can execute a simple CGI script in the same location
Try a simple CGI script to see if the server is able to execute any CGI scripts at all :
#!/usr/local/bin/python3 print("Content-Type; text/html") print("") print("<html><body><h1>hello</h1></body></html>")
Server fails to execute simple CGI script, giving the same error [FAIL]
Fix 1: Change the data path to be absolute instead of relative:
- data_files = glob.glob("data/*.txt")
+ data_files = glob.glob("/home/delliott/public_html/webapp/data/*.txt")
The problem is that the base of your school webserver is not the same as the base of the simple_httpd.py
server. This means that you will have to provide absolute paths to your data directory instead of relative paths.
Change the following lines:
- data_files = glob.glob("data/*.txt")
+ data_files = glob.glob("/home/<username>/public_html/data/*.txt")
This should now behave as expected (if I understand your problem correctly.)
[Edit]: A quick way to check if your cgi-bin scripts work is to run them from the command line. Log into your school server and try the cgi script ion its own:
$ cd /home/<username>/public_html/
$ python3 my_cgi_script.py
This should print out the html that you expect, or a stack trace.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.