Parse a query in the environment or from a file (default stdin) Arguments, all optional: fp : file pointer; default: sys.stdin.buffer environ : environment dictionary; default: os.environ keep_blank_values: flag indicating whether blan
(fp=None, environ=os.environ, keep_blank_values=0,
strict_parsing=0, separator='&')
| 127 | maxlen = 0 |
| 128 | |
| 129 | def parse(fp=None, environ=os.environ, keep_blank_values=0, |
| 130 | strict_parsing=0, separator='&'): |
| 131 | """Parse a query in the environment or from a file (default stdin) |
| 132 | |
| 133 | Arguments, all optional: |
| 134 | |
| 135 | fp : file pointer; default: sys.stdin.buffer |
| 136 | |
| 137 | environ : environment dictionary; default: os.environ |
| 138 | |
| 139 | keep_blank_values: flag indicating whether blank values in |
| 140 | percent-encoded forms should be treated as blank strings. |
| 141 | A true value indicates that blanks should be retained as |
| 142 | blank strings. The default false value indicates that |
| 143 | blank values are to be ignored and treated as if they were |
| 144 | not included. |
| 145 | |
| 146 | strict_parsing: flag indicating what to do with parsing errors. |
| 147 | If false (the default), errors are silently ignored. |
| 148 | If true, errors raise a ValueError exception. |
| 149 | |
| 150 | separator: str. The symbol to use for separating the query arguments. |
| 151 | Defaults to &. |
| 152 | """ |
| 153 | if fp is None: |
| 154 | fp = sys.stdin |
| 155 | |
| 156 | # field keys and values (except for files) are returned as strings |
| 157 | # an encoding is required to decode the bytes read from self.fp |
| 158 | if hasattr(fp,'encoding'): |
| 159 | encoding = fp.encoding |
| 160 | else: |
| 161 | encoding = 'latin-1' |
| 162 | |
| 163 | # fp.read() must return bytes |
| 164 | if isinstance(fp, TextIOWrapper): |
| 165 | fp = fp.buffer |
| 166 | |
| 167 | if not 'REQUEST_METHOD' in environ: |
| 168 | environ['REQUEST_METHOD'] = 'GET' # For testing stand-alone |
| 169 | if environ['REQUEST_METHOD'] == 'POST': |
| 170 | ctype, pdict = parse_header(environ['CONTENT_TYPE']) |
| 171 | if ctype == 'multipart/form-data': |
| 172 | return parse_multipart(fp, pdict, separator=separator) |
| 173 | elif ctype == 'application/x-www-form-urlencoded': |
| 174 | clength = int(environ['CONTENT_LENGTH']) |
| 175 | if maxlen and clength > maxlen: |
| 176 | raise ValueError('Maximum content length exceeded') |
| 177 | qs = fp.read(clength).decode(encoding) |
| 178 | else: |
| 179 | qs = '' # Unknown content-type |
| 180 | if 'QUERY_STRING' in environ: |
| 181 | if qs: qs = qs + '&' |
| 182 | qs = qs + environ['QUERY_STRING'] |
| 183 | elif sys.argv[1:]: |
| 184 | if qs: qs = qs + '&' |
| 185 | qs = qs + sys.argv[1] |
| 186 | environ['QUERY_STRING'] = qs # XXX Shouldn't, really |
nothing calls this directly
no test coverage detected