Created
December 13, 2011 13:46
-
-
Save timesking/1472173 to your computer and use it in GitHub Desktop.
look for and parse miscellaneous GET POST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Alexander Sherwin | |
# 08/14/2005 v1.2 | |
# | |
# This class is designed to look for and parse miscellaneous <strong class="highlight">data</strong> | |
# that is not handled internally through <strong class="highlight">Python</strong> (i.e. <strong class="highlight">POST</strong>, GET.) | |
# | |
# Currently, upon instantiation of the class it automatically parses | |
# all <strong class="highlight">POST</strong>, GET and SERVER <strong class="highlight">data</strong> available to the script. | |
# | |
# Report any errors, bugs or contributions & additions | |
# to [email protected]. This class has been developed and tested on | |
# nearlyfreespeech.net servers (FreeBSD, Apache 2.x.) | |
import sys | |
import os | |
class HTTPWrap: | |
"""Takes the sys.stdin sent from the webserver and parses it for <strong class="highlight">POST</strong> variables | |
This class may not work if your webserver sends other types of information to | |
serverside scripts as sys.stdin | |
.process_post() - Initiates the parsing of raw <strong class="highlight">data</strong> in std.in | |
.process_get() - Initiates the parsing of ENV var QUERY_STRING | |
.server(str) - Checks SERVER vars in server_dict and returns the value | |
- if the key exists, False if it doesn't | |
.post_keys - List of <strong class="highlight">POST</strong> keys (form names) | |
.post_vals - List of <strong class="highlight">POST</strong> values (form values) | |
.post_dict - Dict of <strong class="highlight">POST</strong> keys and values | |
.get_keys - List of GET keys | |
.get_vals - List of GET values | |
.get_dict - Dict of GET keys and values | |
.server_keys - List of SERVER keys | |
.server_vals - List of SERVER values | |
.server_dict - Dict of SERVER keys and values | |
.has_post - True if script has received <strong class="highlight">POST</strong> <strong class="highlight">data</strong> | |
.has_get - True if script has received GET <strong class="highlight">data</strong> | |
.server_dict - Dict of SERVER keys and values""" | |
post_keys = [] # init all lists & dictionaries | |
post_vals = [] | |
post_dict = {} | |
get_keys = [] | |
get_vals = [] | |
get_dict = {} | |
server_keys = [] | |
server_vals = [] | |
server_dict = {} | |
has_post = False # Instantiates with no <strong class="highlight">POST</strong> vars | |
has_get = False # Instantiates with no GET vars | |
def __init__(self): | |
"""Initialize by determining if there is any <strong class="highlight">POST</strong> or GET vars""" | |
self.__raw = sys.stdin.readline() # read from sys.stdin | |
if len(self.__raw): | |
self.process_post() # if <strong class="highlight">POST</strong> string length > 0, parse it | |
self.__raw = "" # init empty string | |
for k in os.environ.items(): | |
if "QUERY_STRING" in k: # find GET <strong class="highlight">data</strong> | |
self.__raw = k[1] # save GET <strong class="highlight">data</strong> | |
else: | |
self.server_dict[k[0]] = k[1] # save SERVER vars | |
self.server_keys.append(k[0]) | |
self.server_vals.append(k[1]) | |
if len(self.__raw): | |
self.process_get() # if GET string length > 0, parse it | |
def server(self, serverVar): | |
"""Check server_dict for a server variable, return it's value""" | |
if self.server_dict.has_key(serverVar): | |
return self.server_dict[serverVar] # return value | |
else: return False | |
def process_post(self): | |
"""Read sys.stdin and parse it to obtain orderly <strong class="highlight">POST</strong> <strong class="highlight">data</strong>""" | |
self.has_post = True # set boolean for exterior uses | |
rawlist = self.__raw.split('&') # obtain list of key=val strings | |
for k in rawlist: | |
tmpli = k.split('=') # get key, val list | |
self.post_keys.append(tmpli[0]) # save key | |
self.post_vals.append(tmpli[1]) # save value | |
self.post_dict[tmpli[0]] = tmpli[1] # save pair in dict | |
def process_get(self): | |
"""Parse self.__raw to obtain orderly GET <strong class="highlight">data</strong>""" | |
self.has_get = True # set boolean for exterior uses | |
rawlist = self.__raw.split('&') # obtain list of key=val strings | |
for k in rawlist: | |
tmpli = k.split('=') # get key, val list | |
self.get_keys.append(tmpli[0]) # save key | |
self.get_vals.append(tmpli[1]) # save value | |
self.get_dict[tmpli[0]] = tmpli[1] # save pair in dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment