Re: cgi.py

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Sun, 10 Jul 1994 01:20:19 -0400

On Jun 21, 11:29, Michael McLay wrote:

>
> "N.G.Smith" writes:
> > I have been using the cgi.py stuff that you posted recently in response
> > to a request I made on the python list. I have found a bug in the
> > unquoting code. It seems to me that it assumes that hex digits will be
> > uppercase. In fact the urllib module escapes using lowercase hex
> > digits. My fix was to replace all the conversion code with a call to
> > urllib.unquote.
>
> Thanks for the bug report. The fix also eliminated some ugly code. I've
> incorporated your suggestion and attached the update.
>

[...]

> nv = string.splitfields(name_value, '=')
> name = nv[0]
> value = urllib.unquote(nv[1])

Unquoting the "%" hex escapes in the string before unescaping the
additional space to "+" that FORMS adds make it impossible to
later disambiguate escaped spaces ( "+" ) from the character "+"
(i.e. The FORM value "Tristan + Dan" was unquoting to "Tristan+++Dan")

So I added the functions:

def enspace( str ):
r = ''
for c in str:
if c == '+' : c = ' '
r = r + c
return r

def unescape( str ):
return urllib.unquote( enspace( str ) )

And replaced the line:
> value = urllib.unquote(nv[1])

with:
value = unescape( str )

Which seems to make it work correctly.

I also added the useful utility routine:

def wrap( co, s ):
return "<" + co + ">\n" + s + "\n</" + co + ">\n"

called as in:
print wrap( 'HD2', "The Query: " )

------
Some other questions that come to mind that I won't be able
to get to for a couple of weeks:

A printlist() that formats python lists as HTML LISTS and
python dictionaries as HTML DEFINITION LISTS would also be
useful

>>> a='<sdm7g@Virginia.EDU>'
>>> print regsub.gsub( '>', '&gt;', regsub.gsub( '<','&lt;',a))
&lt;sdm7g@Virginia.EDU&gt;

Works find if you already have a string, but can anyone thing
of a way to globally change ALL the repr's so that all expressions
printed would be globally changed ? The you could print lists,
dictionaries, ( <Class instances> ), etc. in a python CGI script
without worry!

( Come to think of it, this might be a good job for one of my
"fake file" classes or 'tofile()' functions! i.e. put everything
that produces any output into a main() function, and call
tofile() with 'main' and a file-like Class that filters out
the unallowed characters! -- convert newlines to '<BR>', etc. )

Anybody else have any good Python WWW utilities ?
( besides, of course, the stuff in the library already: urllib &
httplib, plus Michael's cgi.py )

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics