1

I have seen many other answers and tried but they arent helping.

My Python code

#!/usr/bin/env python3
print("Content-Type: text/html")
print()
print ("""
    <TITLE>CGI script ! Python</TITLE>
    <H1>This is my first CGI script</H1>
 """)

Its location and permission

-rwxr-xr-x. 1 root   root   161 May 24 02:42 mypython.py
[root@server cgi-bin]# pwd
/var/www/mysite.com/cgi-bin

Virtual-Host conf

[root@server cgi-bin]# cat /etc/httpd/sites-available/mysite.com.conf
<VirtualHost *:80>
    ServerName www.mysite.com
    DocumentRoot /var/www/mysite.com/public_html
    ServerAlias mysite.com
    ScriptAlias /cgi-bin/ "/var/www/mysite.com/cgi-bin/"
    <Directory /var/www/mysite.com>
        Options Indexes FollowSymLinks Includes ExecCGI
        AddHandler cgi-script .cgi .py
        AllowOverride None
        Require all granted
    </Directory>
    ErrorLog /var/log/httpd/mysite.com/error.log
    CustomLog /var/log/httpd/mysite.com/requests.log combined
</VirtualHost>
[root@server cgi-bin]#

While trying to access , I am getting below error

[Wed May 24 02:42:53.958318 2017] [cgid:error] [pid 7943] [client 192.168.56.1:52390] End of script output before headers: mypython.py
[Wed May 24 02:42:54.661338 2017] [cgid:error] [pid 7939] [client 192.168.56.1:52391] End of script output before headers: mypython.py
[Wed May 24 02:42:59.383215 2017] [cgid:error] [pid 7940] [client 192.168.56.1:52392] End of script output before headers: mypython.py

Please let me know if more information required.

Thank you.

2 Answers 2

1

I suspect your problem might be the

print()

In Python 2.7.12 at least this prints () and not a blank line as you might expect

Please post the exact output of the script as it is right now.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all I take it you web-server is running as root otherwise your execute file permission is wrong.

Secondly if you run the file from the command line it outputs:

Content-Type: text/html


    <TITLE>CGI script ! Python</TITLE>
    <H1>This is my first CGI script</H1>

Note there are two empty lines not one, this is bad as there should be only one see https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html

But if you change your code slightly to remove the other empty line it works.

print ("""<TITLE>CGI script ! Python</TITLE>
    <H1>This is my first CGI script</H1>
 """)

1 Comment

I have removed the empty line but its not working still. Please help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.