[Tutor] python cgi and html streams

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri May 5 23:49:34 CEST 2006


> I was hoping for something magical like:
>
> gulp = cgi.StreamIO("
> http://www.foo.com/cgi-bin/responder.cgi?foo=hi&bar=there&bat=buddy").read()
>
> ... but for some reason not one of the python creators foresaw that I might
> one day need them to do all the thinking, typing and other hard parts for
> me! </rimshot>


Hi Doug,

Try the 'urllib' client library.

     http://www.python.org/doc/lib/module-urllib.html

The CGI server library is meant for servers, say, for the person who 
would be implmenting responder.cgi.

If you need to do more complicated interactions as a client, then 
something like Mechanize might also do the trick:

     http://wwwsearch.sourceforge.net/mechanize/

but I'd recommend looking into urllib first to see if it's sufficient for 
your problem.



> Oho! StringIO seems like it is my baby:
>
> dir(cgi.StringIO)
> ['__doc__', '__init__', '__iter__', '__module__', 'close', 'flush',
> 'getvalue', 'isatty', 'next', 'read', 'readline', 'readlines', 'seek',
> 'tell', 'truncate', 'write', 'writelines']

Gotta cut you off there.  This isn't what you want: it just turns out that 
the implementation of 'cgi' uses the StringIO Standard Library:

     http://www.python.org/doc/lib/module-StringIO.html

but that's an internal detail of 'cgi' that you shouldn't need to worry 
about.


> Am I on the right track here? I suspect there is some sort of super easy 
> way to accomplish this, but I am probably missing it.

You are on the wrong track.  *wink*

No, you were sorta in the right direction, and your instincts are good. 
But next time, try looking at the documentation too --- it's good to 
practice using the reference docs.  The documentation's at:

     http://www.python.org/doc/lib/

The Python Cookbook is also a good place to find out which libraries are 
relevant to the thing you're doing:

     http://aspn.activestate.com/ASPN/Python/Cookbook/

Best of wishes!


More information about the Tutor mailing list