I am running to a problem a vps I recently moved to. I am trying to run a python cgi script, but I am getting an apache Premature end of script headers Error.
(I chmod +x the script file)
The script is pretty simple:
#!/usr/bin/env python
import cgi, cgitb
cgitb.enable()
print "Content-type: text/html"
print "<html><body>hello scritp</body></html>"
Now if I name the script as test**.py** it runs fine on server. But if I do it the correct way, calling it test**.cgi** I receive a Internal Server Error.
I run the script from the terminal
./test.cgi
I get no errors
Content-type: text/html
<html><body>hello scritp</body></html>
Did anyone encountered before this issue? And a solution for it? :)
Cheers
change the header as:
print "Content-type: text/html\n\n"
There must be at least an empty line between HTTP headers and body.
So
print "Content-type: text/html\n" will work just fine
Reference: Wikipedia
Related
I'm trying to run a simple python script on my webserver, but it's not showing up in the web browser.
In terminal I check if python is installed:
whereis python
python: /usr/bin/python2.7 /usr/bin/python2.7-config /usr/bin/python /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/local/bin/python3.9-config /usr/local/bin/python3.9 /usr/local/lib/python3.9 /usr/include/python2.7 /opt/imh-python/bin/python2.7 /opt/imh-python/bin/python2.7-config /opt/imh-python/bin/python3.9 /opt/imh-python/bin/python /usr/share/man/man1/python.1.gz
This tells me that I have python installed. I created a simple file that contains this code:
#! /usr/bin/python
print('Content-Type: text/html\r\n\r\n')
print('\r\n')
print('Hello World')
I ran dos2unix and chmod a+x on the file.
I ran the file in terminal and get this output:
Content-Type: text/html
Hello World
When I try to open the file in the web browser this is the output I get:
#! /usr/bin/python
print('Content-Type: text/html\r\n\r\n')
print('\r\n')
print('Hello World')
I changed the single quotes in the print statement to double. I tried different ways of entering new lines, but nothing seems to work. Am I missing or overlooking something crucial here?
The browser doesn't have a Python interpreter. So opening the file in a browser is just going to show your source code. If you want it to show on a browser you need to run it on a server where it can be interpreted. A simple solution is to use Flask, which comes with a development server. Once you've installed flask:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello World'
app.run()
Then navigate to http://localhost:5000 in your browser.
I am asking it because I write very simply app but IT DON'T WORK.
I wrote this command in terminal in the /dir:
python3 -m http.server --cgi
My script is in dir/cgi-bin/hp.py and that code:
#!/usr/bin/env python3
print"Content-type: text/html"
print()
print"<h1>Hello world!</h1>"
This I saw in window of browser:
Error response
Error code: 403
Message: CGI script is not executable ('/cgi-bin/hp.py').
Error code explanation: HTTPStatus.FORBIDDEN - Request forbidden --
authorization will not help."
How can I fix it?
Here are the following steps which I tried to reproduce the problem:
# app.py
print("Content-type: text/html")
print()
print("<h1>Hello world!</h1>")
Created a file app.py in cgi-bin directory
Used command to run http.server with cgi
python -m http.server --bind localhost --cgi 8000
I tried accessing the path "http:localhost/cgi-bin/" got Error 403
Now the resolving part, which is opening the link in browser.
I ran the command:
python -mwebbrowser http://localhost:8000/cgi-bin/app.py
After the while it gave me the result, and I was able to access the link for the step 2 also.
I hope that helps you.
Result:
I'm getting Internal Server Error from my Python script on Apache.
The script has chmod 755 and is in a directory different from cgi-bin.
Its content is
#!/usr/bin/python
print "Content-Type: text/html\n"
print "test"
I'm on shared hosting with limited options. In particular I cannot view the Apache logfile.
Apache tries to execute the script, but fails to do so because it is missing Options +ExecCGI in the .htaccess located in the script's directory.
It is important to begin the script with the two lines
#!/usr/bin/python
print "Content-Type: text/html\n"
Without those, or without a trailing \n, Apache would throw an Internal Server Error, too.
I can get Python CGI programs to run in other environments but would value help getting to "hello world" with the Cloud9 environment.
My simple programs runs fine, but I cannot get the HTML/web version of even a simple program.
The run gives me a suggestion of:
Your code is running at https://workspacename-username.c9.io.
Important: use os.getenv(PORT, 8080) as the port and os.getenv(IP, 0.0.0.0) as the host in your scripts!
But I do not know what to do with that.
My simple code follows. I also tried to run it as filename.cgi, but that does not work.
#!/usr/bin/env python
print "Content-Type: text/html"
print "<html>"
print "<head><title>My first CGI program</title></head>"
print "<body>"
print "<p> It works!! </p>"
print "</body></html>"
You need to install Flask first, you can do it by the command:
$ sudo easy_install Flask
your code should be smth like this:
import os
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
html= "<html>"
html+= "<head><title>My first CGI program</title></head>"
html+= "<body>"
html+= "<p> It works!! </p>"
html+= "</body></html>"
return html
app.run(host = os.getenv('IP','0.0.0.0'), port=int(os.getenv('PORT',8080)))
After you start application, you'll see
Your code is running at https://workspacename-username.c9users.io.
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
now you can open https://workspacename-username.c9users.io, it will work
I have a system with Ubuntu10.04 Operating System.
I have my Apache tomcat(6.0.29) installed at /usr/local/tomcat/
I have one python script hello.py
I would like to run this python script inside cgi-bin.
Python is at /usr/bin/python
My application I am running is eegis which is running fine.
http://localhost:8080/eegis/index.html
I have already searched in net a lot it is saying you have to configure web.xml with following configuration.
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi</param-value>
</init-param>
<init-param>
<param-name>executable</param-name>
<param-value>/usr/bin/python</param-value>
</init-param>
<init-param>
<param-name>passShellEnvironment</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
I have uncommented this also
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
I have done this but still I am not able to run python script.
While trying http://localhost:8080/eegis/cgi-bin/hello.py
I am getting this error
HTTP Status 404 - /eegis/cgi-bin/
type Status report
message /eegis/cgi-bin/
description The requested resource (/eegis/cgi-bin/) is not available.
Apache Tomcat/6.0.29
The problem has been resolved now. Still I will share my experience. The first problem was with my python script I had forgot to insert "\n\n" (print "Content-type: text/html\n\n") [mid it \n\n immediatetle after header without any spaces (print "Content-type: text/html \n\n") this will not work]at the end of the content description header. Second I have saved the file as .py but it should be .cgi every thing else was fine. My scripts are executing now. Thanks