using python with HTML and parameters

Discussion in 'Python' started by David Stockwell, Jun 30, 2004.

  1. 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?

    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.

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

    Thanks !


    David Stockwell
    -------
    Tracfone: http://cellphone.duneram.com/index.html
    Cam: http://www.duneram.com/cam/index.html
    Tax: http://www.duneram.com/index.html

    _________________________________________________________________
    FREE pop-up blocking with the new MSN Toolbar – get it now!
    http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
     
    David Stockwell, Jun 30, 2004
    #1
    1. Advertising

  2. David Stockwell

    Peter Maas Guest

    David Stockwell schrieb:

    > 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?


    If your Python Script uses CGI it's

    http://somewhere.org/mypage.py?parm1=something&parm2=another

    :) You have to parse the query string with cgi.parse().

    Mit freundlichen Gruessen,

    Peter Maas

    --
    -------------------------------------------------------------------
    Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
    eMail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
    -------------------------------------------------------------------
     
    Peter Maas, Jun 30, 2004
    #2
    1. Advertising

  3. David Stockwell

    Peter Maas Guest

    Peter Maas schrieb:
    > If your Python Script uses CGI it's
    >
    > http://somewhere.org/mypage.py?parm1=something&parm2=another


    Correction: this URL does not depend on CGI, only the Python code.

    Mit freundlichen Gruessen,

    Peter Maas

    --
    -------------------------------------------------------------------
    Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
    eMail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
    -------------------------------------------------------------------
     
    Peter Maas, Jun 30, 2004
    #3
  4. David Stockwell

    John J. Lee Guest

    "David Stockwell" <> writes:

    > 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


    Call me picky <wink>, but that looks like a URL to me, not a piece of
    Java code.


    > How do I do that with python?


    If you mean, "how do I pass parameters to a Python CGI script?", then,
    well, exactly the same way as to any other CGI script. A URL like the
    one you give above, or HTTP POST data (or both).


    > 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.


    No, that's the web server's job.

    However, if you're writing a CGI script that will handle, you may want
    to make use of the standard library cgi module. And, if you're doing
    anything more than a quick hack of a script, I'd advise dropping the
    cgi module and using one of the many web frameworks available for
    Python instead (eg. Quixote).

    HTH


    John
     
    John J. Lee, Jun 30, 2004
    #4
  5. David Stockwell

    Paul Boddie Guest

    "David Stockwell" <> wrote in message news:<>...
    > 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
     
    Paul Boddie, Jul 1, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonck van der Kogel
    Replies:
    2
    Views:
    1,009
    Jonck van der Kogel
    May 27, 2004
  2. Jason
    Replies:
    2
    Views:
    555
    Jonathan Mcdougall
    May 13, 2006
  3. kito
    Replies:
    2
    Views:
    439
  4. Adam Akhtar
    Replies:
    9
    Views:
    594
    Florian Gilcher
    Aug 16, 2008
  5. Florian Loitsch
    Replies:
    11
    Views:
    263
    Michael Winter
    Mar 15, 2005
Loading...

Share This Page