Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting input from a browser to a python script 1

Status
Not open for further replies.

HughJass

Programmer
May 14, 2003
6
US
Hello,

I am new to Python, and I am trying to read in an arbitrary url to a python script. My script is running on an Apache 1.3.27 server, executed by a handler and a corresponding 'Action' directive.

I have looked at urllib.urlopen, but for that, the url must be static and known, or so it would seem. I need to read one into the script on the fly to parse the page and return the same page.

I am trying to do it with f = sys.stdin.read(), then
after running a few functions, I try to return the file
back to the browser by printing to stdout (or just 'print').

I wanted to read in the file, change something, and print the file back to the browser.

Any thoughts?

Thanks
HJ
 
Python does not use a "file" model for a browser. Instead, it has two modules, "os" and "cgi", with most of the functionality. To read an URL request from a browser and to write a response, try something like this:


import cgi
import os

############################
# To read the URL
############################

# Set up an arbitrary dictionary
StoreCentral = {}

# Read in the browser request and fields
StoreCentral['environ'] = os.environ
if StoreCentral['environ'].has_key('PATH_INFO'):
PathInfo = os.environ['PATH_INFO']

# Read in the fields in the URL
StoreCentral['Fields']=cgi.FieldStorage(keep_blank_values=1)

# Get a field called "Ref"
# NB . Use the ".value" format because cgi.FieldStorage is
# predefined in module CGI and requires it.
if StoreCentral['Fields'].has_value('Ref'):
Ref = StoreCentral['Fields']['Ref'].value

############################
# Write back the "Ref" field to the browser
############################

# Place output in to a string for ease
PrintOutput = ''

# Print "headers" so browser knows what to do with
# coming text. Note the compulsory carriage returns.
PrintOutput = "Content-Type: text/html"
PrintOutput += '\n\n'

# Fill in conventional browser text
PrintOutput + '<body><p>Ref is %s</p></body>' % ( str(Ref) )

# Now print it to the browser
print PrintOutput


Note you could have &quot;print&quot;ed directly, instead of adding to the &quot;PrintOutput&quot; string. I just find it easier to do it this way. Not also that there was a check that a field existed before trying to access it. If the field did not exist when trying to access it, the program would have raised an exception. In the context of a browser, this would have given simply a &quot;server error&quot; output in the browser, which is horrendous to handle. Similarly for trying to print out a &quot;Ref&quot; which is an integer without converting it to a string first. So a tip is to use a great Python module &quot;cgitb&quot;. To use it, simply add this code:

import cgitb

cgitb.enable()

Errors are now handled ever so well.

Good luck
 
Thanks Nigel!

I will try your suggestions. I forgot to mention that I am running this script on a UNIX kernal, but I don't think I see anything in your snippet that would be an affront to the kernal.

-Hugh J
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top