Joe Platano
Joe Platano

Reputation: 614

python3 validate elements in CGI.FieldStorage

A CGI script gets some GET data like

test.cgi?key1=val1&key2=val2

The resulting Fieldstorage object looks like

FieldStorage(None, None, [MiniFieldStorage('key1', 'val1'), MiniFieldStorage('key2', 'val2')], MiniFieldStorage('key3', 'val3')])

I have validation lists which are looking like this:

validate_list1 = ('a', 'b', 'c', 'd')
validate_list2 = ('w', 'x', 'y', 'z')
validate_list3 = ('m', 'n', 'o', 'p')

The GET Parameters can be up to 3 key value pairs.

If only one key value pair is returned, i just need to check validate_list1. If Key1 and Key2 are in the CGI From i want to check like this

val1 in validate_list1 AND val2 in validate_list2

and for 3 CGI Parameters

val1 in validate_list1 AND val2 in validate_list2  AND val3 in validate_list3 

How can I evaluate the GET values from the MiniFieldStorage against the associated validate_lists?

Upvotes: 0

Views: 456

Answers (1)

Martinbaste
Martinbaste

Reputation: 416

You can test if a parameter is in the GET request with the in keyword:

keys = ('key1', 'key2', 'key3')
validate_lists = (('a', 'b', 'c', 'd'), ('w', 'x', 'y', 'z'), ('m', 'n', 'o', 'p'))

valid = True
for key, valid in keys, validate_lists:
    if key in cgi.FieldStorage():
        if not cgi.FieldStorage()[key].value in valid:
            valid = False

Upvotes: 1

Related Questions