Connecting Tech Pros Worldwide Help | Site Map

Get parameters from the URL

  #1  
Old August 21st, 2008, 06:05 AM
Sohrab Pawar
Guest
 
Posts: n/a
Hi all,

I'm a python noob so don't be mean :)
I have a URL like http://mySite.net?userID=398&age=28
I'm trying to create a new python script that is called when a user
click a button "Redirect". When a user clicks the "Redirect" button, I
want to send the user to either of the 2 pages, depending on the age of
the user (sent as a parameter using the URL).

What is the best way to
1. Read the URL and get the parameters (age=28 in this case)
2. Redirect the the user accordingly

I have done this kind of thing in a jsp/servlet environment but I have
no idea how to proceed in the Python world.

Any advice/example would help.

Regards,
Sohrab
  #2  
Old August 21st, 2008, 06:15 AM
Marc 'BlackJack' Rintsch
Guest
 
Posts: n/a

re: Get parameters from the URL


On Thu, 21 Aug 2008 10:04:35 +0530, Sohrab Pawar wrote:
Quote:
I'm a python noob so don't be mean :) I have a URL like
http://mySite.net?userID=398&age=28 I'm trying to create a new python
script that is called when a user click a button "Redirect". When a user
clicks the "Redirect" button, I want to send the user to either of the 2
pages, depending on the age of the user (sent as a parameter using the
URL).
>
What is the best way to
1. Read the URL and get the parameters (age=28 in this case) 2. Redirect
the the user accordingly
>
I have done this kind of thing in a jsp/servlet environment but I have
no idea how to proceed in the Python world.
If you really got that string you can use the modules `urlparse` and
`cgi` to get the query part:

In [48]: url = 'http://mySite.net?userID=398&age=28'

In [49]: urlparse.urlparse(url)
Out[49]: ('http', 'mySite.net', '', '', 'userID=398&age=28', '')

In [50]: cgi.parse_qs(urlparse.urlparse(url)[4])
Out[50]: {'age': ['28'], 'userID': ['398']}

But maybe you should read about the `cgi` module and how to use it.

Ciao,
Marc 'BlackJack' Rintsch
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Parameters From URL to Web Form steve2856 answers 1 September 6th, 2007 01:49 PM
Get parameters from URL using CGI abcd answers 3 February 19th, 2006 02:25 PM
Passing parameters from a Web page to a custom control Joe Bloggs answers 3 November 16th, 2005 12:38 PM
Passing arguments to python from URL Casey Bralla answers 1 July 18th, 2005 11:59 PM