Internal Server Error 500 - Python, CGI - python

My .py file executes ok in terminal, but gives this error in the browser
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
...
...
Here is the .py file:
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
print "Content-Type: text/html\n\n" # HTML is following
print # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"
print "<H1>This is my first CGI script</H1>"
print "Hello, world!"
Should i be saving this as a .cgi file? I have tried with the same errors, i have tried many files like this and none work, i am sure the apache server is working as there are other .cgi scripts running from the same directory without issues.
I have also tried:
#!/usr/local/bin/python &
#!/usr/bin/local/python
Any help appreciated.
EDIT
error log output:
(2) No such file or directory: exec of '.../.../.../test.py' failed
Premature end of script headers: test.py

Here is something I wrote up a while ago. These are some good things to look for when troubleshooting Python CGI.
There are some tips to getting Python working in CGI.
Apache setup: This may be old
Add python as a CGI by modifying the following in the configuration:
Options Indexes FollowSymLinks ExecCGI
AddHandler cgi-script .cgi .py
Always browse the pages through Apache.
Note that viewing files in the filesystem through a browser works for most things on an html page but will not work for CGI. For scripts to work they must be opened through the htdocs file system. The address line of your browser should look like:
\\127.0.0.1\index.html or
\\localhost\index.html
If you open a file up through the file system the CGI will not work. Such as if this is in the location bar of your browser:
c:\Apache\htdocs\index.html (or some other example location)
Convert end of lines of scripts to Unix format:
Most editors have options to "show end of lines" and then a tool to convert from Unix to PC format. You must have the end of lines set to Unix format.
State the path to the Python interpreter on the first line of the CGI script:
You must have one of the following lines as the first line of your Python CGI script:
#!C:\Python25\Python.exe
#!/usr/bin/python
The top line is used when you are debugging on a PC and the bottom is for a server such as 1and1. I leave the lines as shown and then edit them once they are up on the server by deleting the first line.
Print a content type specifying HTML before printing any other output:
This can be done simply by adding the following line somewhere very early in your script:
print "Content-Type: text/html\n\n"
Note that 2 end of lines are required.
Setup Python scripts to give debugging information:
Import the following to get detailed debugging information.
import cgitb; cgitb.enable()
An alternative if cgitb is not available is to do the following:
import sys
sys.stderr = sys.stdout
On the server the python script permissions must be set to execute.
After uploading your files be sure to edit the first line and set the permissions for the file to execute.
Check to see if you can hit the python script directly. If you can't, fix with the above steps (2-6). Then when the Python script is working, debug the shtml.

Related

How to fix "Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI." in Python 3.7 http.server?

Environment:
Python 3.7.7
Windows 10 64bits
Purpose of the code:
Display an HTML report of my software activity. It uses http.server module loaded by file Report.py and the data report are extracted by index.py.
I have a script myscript.py which launches the server by calling a method StartReportTool() inside the module Report.py.
Report.py launch the server and load the index.py.
Files & Folder tree:
- myscript.py <= call the method in module Report.py to launch web server
- /report
- /report/Report.py <= launch the web server
- /report/index.py <= extract the data and display them in html
Source code:
myscript.py:
def report():
from report import Report
Report.StartReportTool()
report()
/report/Report.py
#coding:utf-8
import http.server
import webbrowser
def StartReportTool():
port=8888
address=("",port)
server=http.server.HTTPServer
handler=http.server.CGIHTTPRequestHandler
handler.cgi_directories=["/"]
httpd=server(address,handler)
print(f"Report tool server started on port {port}")
webbrowser.open('http://localhost:8888/index.py', new=2)
httpd.serve_forever()
index.py:
#coding:utf-8
import cgi
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
print("Content-type: text/html; charset=utf-8\n")
html=f"""
<!DOCTYPE html>
My html code
"""
Problem:
When I run myscript.py which launches my http.server, my browser open url 'http://localhost:8888/index.py' and show error 404:
Error response
Error code: 404
Message: No such CGI script ('//index.py').
Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI.
After investigation, I realized everything is fine when the server is launched by its own script Report.py:
#coding:utf-8
import http.server
import webbrowser
def StartReportTool():
port=8888
address=("",port)
server=http.server.HTTPServer
handler=http.server.CGIHTTPRequestHandler
handler.cgi_directories=["/"]
httpd=server(address,handler)
print(f"Report tool server started on port {port}")
webbrowser.open('http://localhost:8888/index.py', new=2)
httpd.serve_forever()
StartReportTool() # <======= Here is the line of code which launch the server itself inside the same module
You noticed here the last line of code StartReportTool() which calls the method inside the module itself. And it is working fine by this way. My index.py is loaded correctly.
The problem comes when the method StartReportTool() is called from outside the method.
I don't understand the reasons for the issue. Does anyone understand the source of the problem, please?

malformed header from script index.py Bad header

I want to run python code in apache2(ubuntu 14.04) server. I have followed these steps and getting Error:
Step 1:
Configuration 1: I have created a directory and file under
/var/www/cgi-bin
Configuration 2 : I have edited /etc/apache2/sites-available/000-default.conf
Alias /cgi-bin /var/www/cgi-bin
<Directory "/var/www/cgi-bin">
Options Indexes FollowSymLinks ExecCGI
AddHandler cgi-script .cgi .py
Allow from all
</Directory>
<Directory /var/www/cgi-bin>
Options All
</Directory>
Step 2:
and my python script is: index.py
#!/usr/bin/env python
import cgi;
import cgitb;cgitb.enable()
print "Content-Type: text/plain\n"
print "<b>Hello python</b>"
step 3:
When i ran through chrome browser using:
URL : http://localhost/cgi-bin/index.py
step 4:
I am getting this Error in error-log
malformed header from script 'index.py': Bad header: Hello Python
You should end your header with \r\n, then you must print out yet another \r\n to signal that the body is coming.
(In other words, it's interpreting your body as a Header because the headers were never terminated)
Try this script
#!/usr/bin/env python
import cgi;
import cgitb;cgitb.enable()
print "Content-Type: text/html"
print "" #use this double quote print statement to add a blank line in the script
print "<b>Hello python</b>"
There should be one line space between header and main html content. That's why we have to use extra print statement before starting html tags in script.
I had this issue with the flush mechanism when you need to print a file.
This code responds to a http request if it is called via e.g. apache2.
import sys
print("Content-type: image/png", end="\r\n\r\n", flush=True)
sys.stdout.buffer.write(bytes(open("file.png","rb").read()))
end="\r\n\r\n" adds an empty line to begin the body
flush=True forces python to print the lines as intended. In my case, the header was printed wrong.

How to use AJAX to receive content from Python

I am trying to use AJAX to update a table with data returned from a Python script. When I request the Python script using AJAX, the returned text is the entire python script file, not just the content in the print commands.
My AJAX file:
...standard loadXMLDoc function with callback from W3C AJAX tutorial...
function doNow()
{
loadXMLDoc("cgi-bin/get.py",function()
{
if (request.readyState==4 && request.status==200)
{
document.getElementById("active_items").innerHTML=request.responseText;
}
});
}
window.onload=doNow();
For simplicity, I've used python files as simple as:
print("<div>something</div>")
or
import cgi
import cgitb
cgitb.enable(display=0, logdir="/path/to/logdir")
if __name__ == "__main__":
print("Content-type:text/html\n\n")
print("<div>something</div>")
When I load the page, the content of <div id="active_items"> is:
print("
something
")
I have already:
Set the get.py file to executable using chmod 755 get.py
Verified that CGI privileges are enabled on my server (i.e. other .cgi scripts work)
Verified that my AJAX script works with other static files, such as a .txt file
I'm sure I'm missing something obvious, but I would love some help!
Add to the top of your Python file (assumes *nix environment):
#!/usr/bin/env python
If you haven't already, ensure that your web server treats .py Python scripts as CGI scripts. An example for Apache is this directive:
<Directory /srv/www/yoursite/public_html>
Options +ExecCGI
AddHandler cgi-script .py
</Directory>

Error in python script running in xampp windows

I am new to PYTHON and usually code on PHP. This is the first script I am trying to run on Windows XAMPP. I enabled addhandler for .py and trying to run the following script:
#!C:\Python33\python.exe
# enable debugging
import cgitb
cgitb.enable()
print ("Content-Type: text/html;charset=utf-8")
print ("Hello World!")
and I am getting the following error while running the code:
The server encountered an internal error and was unable to complete your request.
Error message:
malformed header from script 'test.py': Bad header: Hello World!
If you think this is a server error, please contact the webmaster.
You should separate headers from body printing additional newline:
#!C:\Python33\python.exe
import cgitb
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print() # <----------- addtional newlnie for header/body separation.
print("Hello World!")
For anyone who's still getting an error, try replacing print() by print("\r\n") in the following way:
print("Content-Type: text/html;charset=utf-8")
print("\r\n")
print("Hello World!")
This should work!

Python Pages No Rendered

I have created a CGIHTTPServer which works fine, problem is no matter what I do, the python pages are never rendered and the source code is always shown in the browser.
pyhttpd.py
#!/usr/bin/python
import CGIHTTPServer
import BaseHTTPServer
class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
cgi_directories = [""]
PORT = 8080
httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
cgi-bin/hello.py
#!/usr/bin/python
print 'Content-Type: text/html'
print
print '<html>'
print '<head><title>Hello</title></head>'
print '<body>'
print '<h2>Hello World</h2>'
print '</body></html>'
http://some.ip.address:8080/cgi-bin/hello.py:
#!/usr/bin/python
print 'Content-Type: text/html'
print
print '<html>'
print '<head><title>Hello</title></head>'
print '<body>'
print '<h2>Hello World</h2>'
print '</body></html>'
I have set the permission on all files to executable, .html files render fine, even moving the file back to the root folder where the server is running makes no difference, I have tried running as root as well as another normal user, exactly the same results.
Tried googling "python pages not rendered" but have not found anything useful !
EDIT
I have also tried running a simpler server with no overrides, but the result is identical, pything code is never rendered:
pyserv.py
#!/usr/bin/python
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
serve = HTTPServer(("",80),CGIHTTPRequestHandler)
serve.serve_forever()
I believe you're having this issue because you have overridden cgi_directories.
The relevant portion of the documentation reads:
"This defaults to ['/cgi-bin', '/htbin'] and describes directories to treat as containing CGI scripts."
Either place your script in the root directory, or remove the override for cgi_directories and place the scrips in the /cgi-bin directory.
Here is a good link that describes a similar simple setup line by line:
https://pointlessprogramming.wordpress.com/2011/02/13/python-cgi-tutorial-1/
UPDATE:
According to a comment on the above page, it appears that setting cgi_directories = [""] results in disabling the cgi directory feature. Instead, set cgi_directories = ["/"], which sets it to the current directory.

Categories