using python with HTML and parameters

Paul Boddie paul at boddie.net
Thu Jul 1 10:58:20 EDT 2004


"David Stockwell" <winexpert at hotmail.com> wrote in message news:<mailman.302.1088596965.27577.python-list at python.org>...
> Hi,
> 
> In java/jsp I can pass parameters to my python script on a webpage by doing 
> something like this:
> 
> http://somewhere.org/mypage.jsp?parm1=something&parm2=another
> 
> How do I do that with python?

As others have inferred from this question, you're presumably looking
for a way of interpreting the URL's parameters within Python, right?

> Also would I need to import a special module so I could grab them off the 
> 'line'  (I' m not sure what you call it if its a command line, etc as its 
> coming via a 'get' or 'post' request via HTTP protocol.

I believe "request parameters" is the best term to use here.

> On the webpage I am building I would like to pass parameters to python.

Well, your URL will pass two parameters to a resource exposed as
"mypage.jsp" (which could actually be a Python script given the
appropriate Web server configuration, although I suppose you're using
a JSP resource as an example of how you'd do it with Java/JSP); these
parameters would be "parm1" and "parm2", and I suppose you'd use the
Java Servlet API methods getParameter or getParameterValues to get the
associated values which are "something" and "another" respectively for
the specified parameters.

Now, in Python, it really depends on your chosen technology as to how
you get those values. Anything vaguely based on the cgi module
(http://docs.python.org/lib/module-cgi.html) will require you to get
hold of a FieldStorage object and then to use the getvalue method on
it, although care has to be taken about the result and whether it's a
list or not (since you can have many values for a given parameter
name).

In WebStack (http://www.python.org/pypi?:action=display&name=WebStack&version=0.5)
I've tried to standardise this kind of thing, even converting
parameter values from POST requests to Unicode in order to remove
difficulties when actually using such values later on, but on matters
of standardisation the Web-SIG remains silent, so I wouldn't expect
the confusing situation to remedy itself any time soon.

Paul



More information about the Python-list mailing list