Page 1 of 1

Python CGI

PostPosted: Sat Sep 10, 2016 7:18 pm
by GregWallace
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.
http://www.aceway.net/shrink/index.htm

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.
print
# 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>'


Regards
Greg

Re: Python CGI

PostPosted: Sat Sep 10, 2016 9:47 pm
by metulburr
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.

Re: Python CGI

PostPosted: Sun Sep 11, 2016 9:18 pm
by snippsat
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 ;)