Python cgi.FieldStorage() Examples
The following are 30
code examples of cgi.FieldStorage().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cgi
, or try the search function
.
Example #1
Source File: multidict.py From mishkal with GNU General Public License v3.0 | 7 votes |
def _decode_value(self, value): """ Decode the specified value to unicode. Assumes value is a ``str`` or `FieldStorage`` object. ``FieldStorage`` objects are specially handled. """ if isinstance(value, cgi.FieldStorage): # decode FieldStorage's field name and filename value = copy.copy(value) if self.decode_keys: value.name = value.name.decode(self.encoding, self.errors) value.filename = value.filename.decode(self.encoding, self.errors) else: try: value = value.decode(self.encoding, self.errors) except AttributeError: pass return value
Example #2
Source File: simple_web_server.py From Archive-SE with Apache License 2.0 | 6 votes |
def do_POST(self): if self.path == "/play": form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': self.headers['Content-Type'],}) print "Client -> Le tableau: %s" % form["le_tableau"].value i = random.randint(1, 9) while form["le_tableau"].value[i] != "0": # print "index ", i, " value ", form["le_tableau"].value[i] i = random.randint(1, 9) print "Server played %s ->" % i self.send_response(200) self.end_headers() self.wfile.write(i-1) return
Example #3
Source File: simple_web_server.py From Archive-SE with Apache License 2.0 | 6 votes |
def do_POST(self): if self.path=="/send": form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'], }) self.nsa_queue.put(form["le_texte"].value[::-1]) print "Le texte en clair: %s" % form["le_texte"].value self.send_response(200) self.end_headers() self.wfile.write(form["le_texte"].value[::-1]) return if self.path=="/decrypt": le_texte = self.nsa_queue.get() print "Le texte en encode: %s" % le_texte self.send_response(200) self.end_headers() self.wfile.write("Le texte intercepte: %s." % le_texte) self.wfile.write(" Le texte decode: %s" % le_texte[::-1]) return
Example #4
Source File: test_cgi.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_strict(self): for orig, expect in parse_strict_test_cases: # Test basic parsing d = do_test(orig, "GET") self.assertEqual(d, expect, "Error parsing %s method GET" % repr(orig)) d = do_test(orig, "POST") self.assertEqual(d, expect, "Error parsing %s method POST" % repr(orig)) env = {'QUERY_STRING': orig} fs = cgi.FieldStorage(environ=env) if isinstance(expect, dict): # test dict interface self.assertEqual(len(expect), len(fs)) self.assertCountEqual(expect.keys(), fs.keys()) ##self.assertEqual(norm(expect.values()), norm(fs.values())) ##self.assertEqual(norm(expect.items()), norm(fs.items())) self.assertEqual(fs.getvalue("nonexistent field", "default"), "default") # test individual fields for key in expect.keys(): expect_val = expect[key] self.assertIn(key, fs) if len(expect_val) > 1: self.assertEqual(fs.getvalue(key), expect_val) else: self.assertEqual(fs.getvalue(key), expect_val[0])
Example #5
Source File: test_cgi.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_fieldstorage_multipart(self): #Test basic FieldStorage multipart parsing env = { 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY), 'CONTENT_LENGTH': '558'} fp = BytesIO(POSTDATA.encode('latin-1')) fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1") self.assertEqual(len(fs.list), 4) expect = [{'name':'id', 'filename':None, 'value':'1234'}, {'name':'title', 'filename':None, 'value':''}, {'name':'file', 'filename':'test.txt', 'value':b'Testing 123.\n'}, {'name':'submit', 'filename':None, 'value':' Add '}] for x in range(len(fs.list)): for k, exp in expect[x].items(): got = getattr(fs.list[x], k) self.assertEqual(got, exp)
Example #6
Source File: risk_server.py From aswan with GNU Lesser General Public License v2.1 | 6 votes |
def __parse_post_body(environ, ignore_get=False): post_data = {} # accept post json if environ["CONTENT_TYPE"].strip(';') == "application/json" and environ["REQUEST_METHOD"] == "POST": storage = environ['wsgi.input'].read() if storage: return json.loads(storage) storage = FieldStorage(environ['wsgi.input'], environ=environ, keep_blank_values=True) # accept get querystring if not ignore_get: for k in storage.keys(): post_data[k] = storage.getvalue(k) return post_data
Example #7
Source File: test_cgi.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_fieldstorage_multipart_leading_whitespace(self): env = { 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY), 'CONTENT_LENGTH': '560'} # Add some leading whitespace to our post data that will cause the # first line to not be the innerboundary. fp = BytesIO(b"\r\n" + POSTDATA.encode('latin-1')) fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1") self.assertEqual(len(fs.list), 4) expect = [{'name':'id', 'filename':None, 'value':'1234'}, {'name':'title', 'filename':None, 'value':''}, {'name':'file', 'filename':'test.txt', 'value':b'Testing 123.\n'}, {'name':'submit', 'filename':None, 'value':' Add '}] for x in range(len(fs.list)): for k, exp in expect[x].items(): got = getattr(fs.list[x], k) self.assertEqual(got, exp)
Example #8
Source File: fcgi.py From pycopia with Apache License 2.0 | 6 votes |
def test_app(environ, start_response): """Probably not the most efficient example.""" import cgi start_response('200 OK', [('Content-Type', 'text/html')]) yield '<html><head><title>Hello World!</title></head>\n' \ '<body>\n' \ '<p>Hello World!</p>\n' \ '<table border="1">' names = environ.keys() names.sort() for name in names: yield '<tr><td>%s</td><td>%s</td></tr>\n' % ( name, cgi.escape(`environ[name]`)) form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ, keep_blank_values=1) if form.list: yield '<tr><th colspan="2">Form data</th></tr>' for field in form.list: yield '<tr><td>%s</td><td>%s</td></tr>\n' % ( field.name, field.value) yield '</table>\n' \ '</body></html>\n'
Example #9
Source File: test_cgi.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_fieldstorage_multipart_w3c(self): # Test basic FieldStorage multipart parsing (W3C sample) env = { 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY_W3), 'CONTENT_LENGTH': str(len(POSTDATA_W3))} fp = BytesIO(POSTDATA_W3.encode('latin-1')) fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1") self.assertEqual(len(fs.list), 2) self.assertEqual(fs.list[0].name, 'submit-name') self.assertEqual(fs.list[0].value, 'Larry') self.assertEqual(fs.list[1].name, 'files') files = fs.list[1].value self.assertEqual(len(files), 2) expect = [{'name': None, 'filename': 'file1.txt', 'value': b'... contents of file1.txt ...'}, {'name': None, 'filename': 'file2.gif', 'value': b'...contents of file2.gif...'}] for x in range(len(files)): for k, exp in expect[x].items(): got = getattr(files[x], k) self.assertEqual(got, exp)
Example #10
Source File: test_cgi.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_fieldstorage_part_content_length(self): BOUNDARY = "JfISa01" POSTDATA = """--JfISa01 Content-Disposition: form-data; name="submit-name" Content-Length: 5 Larry --JfISa01""" env = { 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart/form-data; boundary={}'.format(BOUNDARY), 'CONTENT_LENGTH': str(len(POSTDATA))} fp = BytesIO(POSTDATA.encode('latin-1')) fs = cgi.FieldStorage(fp, environ=env, encoding="latin-1") self.assertEqual(len(fs.list), 1) self.assertEqual(fs.list[0].name, 'submit-name') self.assertEqual(fs.list[0].value, 'Larry')
Example #11
Source File: bottle2.py From pyFileFixity with MIT License | 6 votes |
def POST(self): """ The HTTP POST body parsed into a MultiDict. This supports urlencoded and multipart POST requests. Multipart is commonly used for file uploads and may result in some of the values beeing cgi.FieldStorage objects instead of strings. Multiple values per key are possible. See MultiDict for details. """ if self._POST is None: save_env = dict() # Build a save environment for cgi for key in ('REQUEST_METHOD', 'CONTENT_TYPE', 'CONTENT_LENGTH'): if key in self.environ: save_env[key] = self.environ[key] save_env['QUERY_STRING'] = '' # Without this, sys.argv is called! if TextIOWrapper: fb = TextIOWrapper(self.body, encoding='ISO-8859-1') else: fb = self.body data = cgi.FieldStorage(fp=fb, environ=save_env) self._POST = MultiDict() for item in data.list: self._POST[item.name] = item if item.filename else item.value return self._POST
Example #12
Source File: bottle3.py From pyFileFixity with MIT License | 6 votes |
def POST(self): """ The HTTP POST body parsed into a MultiDict. This supports urlencoded and multipart POST requests. Multipart is commonly used for file uploads and may result in some of the values beeing cgi.FieldStorage objects instead of strings. Multiple values per key are possible. See MultiDict for details. """ if self._POST is None: save_env = dict() # Build a save environment for cgi for key in ('REQUEST_METHOD', 'CONTENT_TYPE', 'CONTENT_LENGTH'): if key in self.environ: save_env[key] = self.environ[key] save_env['QUERY_STRING'] = '' # Without this, sys.argv is called! if TextIOWrapper: fb = TextIOWrapper(self.body, encoding='ISO-8859-1') else: fb = self.body data = cgi.FieldStorage(fp=fb, environ=save_env) self._POST = MultiDict() for item in data.list: self._POST[item.name] = item if item.filename else item.value return self._POST
Example #13
Source File: request.py From pledgeservice with Apache License 2.0 | 6 votes |
def transcode_fs(self, fs, content_type): # transcode FieldStorage if PY3: # pragma: no cover decode = lambda b: b else: decode = lambda b: b.decode(self.charset, self.errors) data = [] for field in fs.list or (): field.name = decode(field.name) if field.filename: field.filename = decode(field.filename) data.append((field.name, field)) else: data.append((field.name, decode(field.value))) # TODO: transcode big requests to temp file content_type, fout = _encode_multipart( data, content_type, fout=io.BytesIO() ) return fout # TODO: remove in 1.4
Example #14
Source File: httpd.py From weeman with GNU General Public License v3.0 | 6 votes |
def do_POST(self): post_request = [] printt(3, "%s - sent POST request." %self.address_string()) form = cgi.FieldStorage(self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'],}) try: from core.shell import url logger = open("%s.log" %url.replace("https://", "").replace("http://", "").split("/")[0], "w+") logger.write("## Data for %s\n\n" %url) for tag in form.list: tmp = str(tag).split("(")[1] key,value = tmp.replace(")", "").replace("\'", "").replace(",", "").split() post_request.append("%s %s" %(key,value)) printt(2, "%s => %s" %(key,value)) logger.write("%s => %s\n" %(key,value)) logger.close() from core.shell import action_url create_post(url,action_url, post_request) SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) except socerr as e: printt(3, "Something wrong: (%s) igonring ..." %str(e)) except Exception as e: printt(3, "Something wrong: (%s) igonring ..." %str(e))
Example #15
Source File: bottle.py From VaspCZ with MIT License | 6 votes |
def files(self): """ File uploads parsed from an `url-encoded` or `multipart/form-data` encoded POST or PUT request body. The values are instances of :class:`cgi.FieldStorage`. The most important attributes are: filename The filename, if specified; otherwise None; this is the client side filename, *not* the file name on which it is stored (that's a temporary file you don't deal with) file The file(-like) object from which you can read the data. value The value as a *string*; for file uploads, this transparently reads the file every time you request the value. Do not do this on big files. """ files = FormsDict() for name, item in self.POST.iterallitems(): if hasattr(item, 'filename'): files[name] = item return files
Example #16
Source File: blobstore_test.py From datastore-ndb-python with Apache License 2.0 | 6 votes |
def testBlobstore_ParseBlobInfo(self): env = {'REQUEST_METHOD': 'POST'} hdrs = {'content-disposition': 'blah; filename=hello.txt; name=hello', 'content-type': 'text/plain; blob-key=xxx'} fd = cStringIO.StringIO( 'Content-type: image/jpeg\n' 'Content-length: 42\n' 'X-AppEngine-Upload-Creation: 2012-01-24 17:35:00.000000\n' 'Content-MD5: eHh4\n' '\n' ) fs = cgi.FieldStorage(fd, headers=hdrs, environ=env) bi = blobstore.parse_blob_info(fs) self.assertTrue(isinstance(bi, blobstore.BlobInfo)) self.assertEqual( bi, blobstore.BlobInfo(key=model.Key(blobstore.BlobInfo, 'xxx'), content_type='image/jpeg', creation=datetime.datetime(2012, 1, 24, 17, 35), filename='hello.txt', md5_hash='xxx', size=42))
Example #17
Source File: handlers.py From quantipy with MIT License | 6 votes |
def do_POST(self): """ Store the result in a file, shut down the serve and then continue the script """ form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'], }) for item in form.list: print item.name if item.name == "obj_json": save_string_in_tmp_folder( data=item.value, filename="obj.json") break shutdown_server(server_target=self.server) SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
Example #18
Source File: bottle.py From VaspCZ with MIT License | 6 votes |
def POST(self): """ The values of :attr:`forms` and :attr:`files` combined into a single :class:`FormsDict`. Values are either strings (form values) or instances of :class:`cgi.FieldStorage` (file uploads). """ post = FormsDict() safe_env = {'QUERY_STRING':''} # Build a safe environment for cgi for key in ('REQUEST_METHOD', 'CONTENT_TYPE', 'CONTENT_LENGTH'): if key in self.environ: safe_env[key] = self.environ[key] if NCTextIOWrapper: fb = NCTextIOWrapper(self.body, encoding='ISO-8859-1', newline='\n') else: fb = self.body data = cgi.FieldStorage(fp=fb, environ=safe_env, keep_blank_values=True) for item in (data.list or [])[:self.MAX_PARAMS]: post[item.name] = item if item.filename else item.value return post
Example #19
Source File: wsgi.py From ariadne with BSD 3-Clause "New" or "Revised" License | 6 votes |
def extract_data_from_multipart_request(self, environ: dict) -> Any: try: form = FieldStorage( fp=environ["wsgi.input"], environ=environ, keep_blank_values=True ) except (TypeError, ValueError): raise HttpBadRequestError("Malformed request data") try: operations = json.loads(form.getvalue("operations")) except (TypeError, ValueError): raise HttpBadRequestError( "Request 'operations' multipart field is not a valid JSON" ) try: files_map = json.loads(form.getvalue("map")) except (TypeError, ValueError): raise HttpBadRequestError( "Request 'map' multipart field is not a valid JSON" ) return combine_multipart_data(operations, files_map, form)
Example #20
Source File: dev_appserver_login.py From browserscope with Apache License 2.0 | 6 votes |
def main(): """Runs the login and logout CGI script.""" form = cgi.FieldStorage(environ=os.environ) login_url = os.environ['PATH_INFO'] email = os.environ.get('USER_EMAIL', '') admin = os.environ.get('USER_IS_ADMIN', '0') == '1' action = form.getfirst(ACTION_PARAM) set_email = form.getfirst(EMAIL_PARAM, '') set_admin = form.getfirst(ADMIN_PARAM, '') == 'True' continue_url = form.getfirst(CONTINUE_PARAM, '') LoginCGI(login_url, email, admin, action, set_email, set_admin, continue_url, sys.stdout) return 0
Example #21
Source File: test_cgi.py From ironpython3 with Apache License 2.0 | 6 votes |
def do_test(buf, method): env = {} if method == "GET": fp = None env['REQUEST_METHOD'] = 'GET' env['QUERY_STRING'] = buf elif method == "POST": fp = BytesIO(buf.encode('latin-1')) # FieldStorage expects bytes env['REQUEST_METHOD'] = 'POST' env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded' env['CONTENT_LENGTH'] = str(len(buf)) else: raise ValueError("unknown method: %s" % method) try: return cgi.parse(fp, env, strict_parsing=1) except Exception as err: return ComparableException(err)
Example #22
Source File: test_cgi.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def do_test(buf, method): env = {} if method == "GET": fp = None env['REQUEST_METHOD'] = 'GET' env['QUERY_STRING'] = buf elif method == "POST": fp = BytesIO(buf.encode('latin-1')) # FieldStorage expects bytes env['REQUEST_METHOD'] = 'POST' env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded' env['CONTENT_LENGTH'] = str(len(buf)) else: raise ValueError("unknown method: %s" % method) try: return cgi.parse(fp, env, strict_parsing=1) except Exception as err: return ComparableException(err)
Example #23
Source File: manager.py From pmatic with GNU General Public License v2.0 | 5 votes |
def getvalue(self, key, default=None): value = cgi.FieldStorage.getvalue(self, key.encode("utf-8"), default) if value is not None: return value.decode("utf-8") else: return None
Example #24
Source File: wsgi.py From slackbridge with GNU General Public License v3.0 | 5 votes |
def get_payload(environ): # We need to read CONTENT_TYPE and REQUEST_METHOD. post_env = environ.copy() post_env['QUERY_STRING'] = '' # we don't want GET data in there return cgi.FieldStorage(fp=environ['wsgi.input'], environ=post_env, keep_blank_values=True)
Example #25
Source File: manager.py From pmatic with GNU General Public License v2.0 | 5 votes |
def __init__(self, manager, environ, start_response): super(PageHandler, self).__init__() self._manager = manager self._env = environ self._start_response = start_response self._http_headers = [] self._set_http_header("Content-type", self._get_content_type()) self._page = [] self._vars = cgi.FieldStorage() self._read_environment() self._is_valid_transaction = False self._check_transaction()
Example #26
Source File: requests.py From kobin with MIT License | 5 votes |
def query(self): params = cgi.FieldStorage( environ=self.environ, keep_blank_values=True, ) p = {k: params[k].value for k in params} return p
Example #27
Source File: test_cgi.py From oss-ftp with MIT License | 5 votes |
def test_fieldstorage_readline(self): # FieldStorage uses readline, which has the capacity to read all # contents of the input file into memory; we use readline's size argument # to prevent that for files that do not contain any newlines in # non-GET/HEAD requests class TestReadlineFile: def __init__(self, file): self.file = file self.numcalls = 0 def readline(self, size=None): self.numcalls += 1 if size: return self.file.readline(size) else: return self.file.readline() def __getattr__(self, name): file = self.__dict__['file'] a = getattr(file, name) if not isinstance(a, int): setattr(self, name, a) return a f = TestReadlineFile(tempfile.TemporaryFile()) f.write('x' * 256 * 1024) f.seek(0) env = {'REQUEST_METHOD':'PUT'} fs = cgi.FieldStorage(fp=f, environ=env) # if we're not chunking properly, readline is only called twice # (by read_binary); if we are chunking properly, it will be called 5 times # as long as the chunksize is 1 << 16. self.assertGreater(f.numcalls, 2)
Example #28
Source File: manager.py From pmatic with GNU General Public License v2.0 | 5 votes |
def _read_vars(self): wsgi_input = self._env["wsgi.input"] if not wsgi_input: return self._vars = FieldStorage(fp=wsgi_input, environ=self._env, keep_blank_values=1)
Example #29
Source File: run_wsgi.py From mako with MIT License | 5 votes |
def serve(environ, start_response): """serves requests using the WSGI callable interface.""" fieldstorage = cgi.FieldStorage( fp = environ['wsgi.input'], environ = environ, keep_blank_values = True ) d = dict([(k, getfield(fieldstorage[k])) for k in fieldstorage]) uri = environ.get('PATH_INFO', '/') if not uri: uri = '/index.html' else: uri = re.sub(r'^/$', '/index.html', uri) if re.match(r'.*\.html$', uri): try: template = lookup.get_template(uri) except exceptions.TopLevelLookupException: start_response("404 Not Found", []) return [str.encode("Cant find template '%s'" % uri)] start_response("200 OK", [('Content-type','text/html')]) try: return [template.render(**d)] except: return [exceptions.html_error_template().render()] else: u = re.sub(r'^\/+', '', uri) filename = os.path.join(root, u) if os.path.isfile(filename): start_response("200 OK", [('Content-type',guess_type(uri))]) return [open(filename, 'rb').read()] else: start_response("404 Not Found", []) return [str.encode("File not found: '%s'" % filename)]
Example #30
Source File: comp.py From kansha with BSD 3-Clause "New" or "Revised" License | 5 votes |
def add_assets(self, new_files): """Add new assets to the card In: - ``new_files`` -- new files to add """ if isinstance(new_files, FieldStorage): self.add_asset(new_files) else: for new_file in new_files: self.add_asset(new_file)