SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old March 9th, 2004, 05:19 PM
roypython roypython is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 71 roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 h 20 m 49 sec
Reputation Power: 5
Reading values from GET using cgi

HI.
I have a bbt of a problem and I would appreciated your help.
Usually to read values, I use
form = cgi.FieldStorage()
and then treat the form variable like a dictionary type
but....
how can you read the values if there are multiple keys, for example
if we write in the HTML
<select Name="lstRsrcs" SIZE='4' MULTIPLE>
<option VALUE="Rsrc1">Rsrc1Desc</option>
<option VALUE="Rsrc2">Rsrc2Desc</option>
<option VALUE="Rsrc3">Rsrc3Desc</option>
</select>
and we select more then one option, the http will look something like:
url ?lstRsrcs=Rsrc1&lstRsrcs=Rsrc2&lstRsrcs=Rsrc3

how can I read that?! or iterate through that?
Thanks a lot
Roy

Reply With Quote
  #2  
Old March 10th, 2004, 03:00 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,188 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 11 h 25 m 43 sec
Reputation Power: 251
If you are using version 2.2 or later you can use the getlist method. From the docs:

Quote:
A more convenient approach is to use the methods getfirst() and getlist() provided by this higher level interface.


getfirst( name[, default])

This method always returns only one value associated with form field name. The method returns only the first value in case that more values were posted under such name. Please note that the order in which the values are received may vary from browser to browser and should not be counted on. If no such form field or value exists then the method returns the value specified by the optional parameter default. This parameter defaults to None if not specified.

getlist( name)

This method always returns a list of values associated with form field name. The method returns an empty list if no such form field or value exists for name. It returns a list consisting of one item if only one such value exists.
Using these methods you can write nice compact code:

Code:
import cgi
form = cgi.FieldStorage()
user = form.getfirst("user", "").toupper()    # This way it's safe.
for item in form.getlist("item"):
    do_something(item)


For versions prior to 2.2, the FieldStorage.getvalue(name) method returns a string if there is only one item and a list if there is more than one, so you have to check explicitly. From the docs again:

Code:
item = form.getvalue("item")
if isinstance(item, list):
    # The user is requesting more than one item.
else:
    # The user is requesting only one item.


Regards,

Dave - The Developers' Coach

Reply With Quote
  #3  
Old March 10th, 2004, 02:05 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I recently had to write a form parser as part of my net module, as part of this it had to accomodate multiple fields with the same value. Anyway i've attached the file; as an example of parsing and use values from GET/QUERY_STRING.

Have fun,

Mark.
Attached Files
File Type: txt params.txt (6.0 KB, 256 views)
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #4  
Old March 11th, 2004, 02:00 PM
roypython roypython is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 71 roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 h 20 m 49 sec
Reputation Power: 5
Thanks Dave

Quote:
Originally Posted by DevCoach
If you are using version 2.2 or later you can use the getlist method. From the docs:



For versions prior to 2.2, the FieldStorage.getvalue(name) method returns a string if there is only one item and a list if there is more than one, so you have to check explicitly. From the docs again:

Code:
item = form.getvalue("item")
if isinstance(item, list):
    # The user is requesting more than one item.
else:
    # The user is requesting only one item.


Regards,

Dave - The Developers' Coach


This is exactly what iv'e done and it works beautiful
thank you for the very good explanation and examples
Roy

Reply With Quote
  #5  
Old March 11th, 2004, 02:04 PM
roypython roypython is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 71 roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 h 20 m 49 sec
Reputation Power: 5
Thanks Mark

Quote:
Originally Posted by netytan
I recently had to write a form parser as part of my net module, as part of this it had to accomodate multiple fields with the same value. Anyway i've attached the file; as an example of parsing and use values from GET/QUERY_STRING.

Have fun,

Mark.

I solved the problem by using form.GetList method that Dave suggested.
I will read your module throughly, and I'm sure that I can learn a lot from that.
Thanks a lot
Roy

Reply With Quote
  #6  
Old March 11th, 2004, 03:04 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
No problem at all roypy, i didnt expect that you would be wrting you're own form parser to solve a simple problem like this but as an example of how a form parser might work it works very well

Take care,

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Reading values from GET using cgi


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway