Python CGI

All new tutorial are submitted here.
They will be moved to the main Tutorials section once they have been approved by mods and admins.

Python CGI

Postby GregWallace » Sat Sep 10, 2016 7:18 pm

This is a copy of what I sent to a friend to solve Python on web issues.
Hi Mick

It's no longer Shrink on web and just getting Python to work on web. Changing to Python 2.7 worked straight away. I assume the problem is the same one I had when trying to run Shrink from the desktop using Python 3.5. So all the samples I found are Python 2. The problem with Python 3.x is the change of syntax for the print command.

When I say worked straight away it gave a web page response in plain text and displayed the html code. I had to eliminate certain lines to get it to work as formatted HTML. It is all trial and error.

So even tho it is not in your current scope, I found it a challenge to make it work as CGI with IIS 7 on Windows 2008 server. I am providing what I learned as documentation in case I or you ever decide to use python on web.

One small syntax error and it gives
"502 - Web server received an invalid response while acting as a gateway or proxy server."
I'm sure there must be a way to give an intelligent syntax error response and indicate the line in error. The lines for cgi and cgitb don't seem to do anything.

Here is the link to the web page.

This page has two methods for running a script.
A Form with submit of fields and an anchor tag which does not pass fields.

Here is the working script.

Code: Select all
# these import lines don't seem to make any difference
import webbrowser
import cgi
import cgitb
; cgitb.enable()  # for troubleshooting

# if I omit this first print blank line it fails.
# commenting the following lines made it work as html output
#print 'Status: 200 OK'
#print 'Content-type: text/html'

print '<HTML><HEAD><TITLE>Python Sample CGI</TITLE>'
# I added this line. If I omit it - it still works
print '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">'
print '</HEAD>'
print '<BODY>'
print '<H1>Mick Godwin Condensing Cloud Model</H1>'

print '<p>' #this is a comment
print 'At this stage, it is just a working infastructure and more can be done.'
print '<br>'
print '</BODY>'

Last edited by micseydel on Sat Sep 10, 2016 7:23 pm, edited 1 time in total.
Reason: Code tags. Initial post lock.
Posts: 1
Joined: Sat Sep 10, 2016 7:15 pm

Re: Python CGI

Postby metulburr » Sat Sep 10, 2016 9:47 pm

I'm sure there must be a way to give an intelligent syntax error response and indicate the line in error. The lines for cgi and cgitb don't seem to do anything.

I dont use Windows servers. I use linux and apache. In which you can simply figure out any error by checking the apache error logs. For a simple syntax error, it shows the traceback in the error log in which you can check in real time. There must be something similar in windows server. But who knows, it is Windows after all.
we will be moving to on October 1 2016
more details here
User avatar
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Python CGI

Postby snippsat » Sun Sep 11, 2016 9:18 pm

Do not use CGI it's thankfully dear after WSGI.
Python use now WSGI for all web-development,this is a solution all written in Python.
WSGI is by design a simple standard interface for running Python code.
As a web developer you won’t need to know much more than.
So not gone talk more about it

So use Flask,which is a modern way and much better way of doing what CGI did ;)
We will be moving to on October 1 2016
User avatar
Posts: 1251
Joined: Thu Feb 21, 2013 12:04 am

Return to Tutorial Submissions

Who is online

Users browsing this forum: No registered users and 1 guest