Python cgi.parse_multipart() Examples
The following are 26
code examples of cgi.parse_multipart().
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: server.py From sparrest with MIT License | 6 votes |
def get_data(self): """ Returns the JSON data converted to a dict depending of the content-type sent. Only if data format is correct, returns the dict, otherwise, returns None """ if self.data is None: if self.is_json_content_type(): try: self.data = json.loads(self.get_content()) except ValueError: self.data = None elif self.is_form_urlencoded_data_content_type(): parsed_data = parse_qs(self.get_content(), keep_blank_values=True) self.data = dict(map( lambda t: (t[0], t[1][0] if type(t[1]) == list and len(t[1]) == 1 else t[1]), parsed_data.items() )) elif self.is_multipart_form_data_content_type(): ctype, pdict = cgi.parse_header(self.headers.get('content-type')) if 'boundary' in pdict: pdict['boundary'] = pdict['boundary'].encode() content = self.get_content(decode=False) filenames = re.findall(r'filename="(.*?)"', str(content), re.IGNORECASE | re.DOTALL | re.MULTILINE) parsed_data = cgi.parse_multipart(BytesIO(content), pdict) self.data = {} for key in parsed_data: parsed_item = parsed_data[key] if type(parsed_item) == list: for content in parsed_item: try: self.data[key] = parsed_item[0].decode('utf-8') except UnicodeDecodeError as e: # we assume that are files try: filename = filenames.pop(0) except IndexError as e: filename = None self.data[key] = self.save_as_file(content, filename) else: self.data[key] = parsed_item return self.data
Example #2
Source File: start.py From ps4-exploit-host with MIT License | 6 votes |
def parse_POST(self): try: ctype, pdict = parse_header(self.headers['content-type']) if ctype == 'multipart/form-data': postvars = parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': length = int(self.headers['content-length']) postvars = parse_qs(self.rfile.read(length), keep_blank_values=1) elif ctype == 'application/json': length = int(self.headers['content-length']) postvars = json.loads(self.rfile.read(length)) else: postvars = {} except (KeyError, TypeError): postvars = {} # TODO: Standardize parsed data return postvars
Example #3
Source File: interactive.py From ParlAI with MIT License | 6 votes |
def do_POST(self): """ Handle POST. """ if self.path != "/interact": return self.respond({"status": 500}) ctype, pdict = cgi.parse_header(self.headers["content-type"]) pdict["boundary"] = bytes(pdict["boundary"], "utf-8") postvars = cgi.parse_multipart(self.rfile, pdict) if postvars["image"][0].decode() != "": SHARED["dialog_history"] = [] SHARED["image_feats"] = None model_response = self.interactive_running(postvars) self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() json_str = json.dumps(model_response) self.wfile.write(bytes(json_str, "utf-8"))
Example #4
Source File: interactive.py From ParlAI with MIT License | 6 votes |
def do_POST(self): """ Handle POST. """ if self.path != '/interact': return self.respond({'status': 500}) ctype, pdict = cgi.parse_header(self.headers['content-type']) pdict['boundary'] = bytes(pdict['boundary'], "utf-8") postvars = cgi.parse_multipart(self.rfile, pdict) model_response = self.interactive_running(postvars) self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() json_str = json.dumps(model_response) self.wfile.write(bytes(json_str, 'utf-8'))
Example #5
Source File: interactive.py From neural_chat with MIT License | 6 votes |
def do_POST(self): """Handle POST.""" if self.path != "/interact": return self.respond({"status": 500}) ctype, pdict = cgi.parse_header(self.headers["content-type"]) pdict["boundary"] = bytes(pdict["boundary"], "utf-8") postvars = cgi.parse_multipart(self.rfile, pdict) if postvars["image"][0].decode() != "": SHARED["dialog_history"] = [] SHARED["image_feats"] = None model_response = self.interactive_running(postvars) self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() json_str = json.dumps(model_response) self.wfile.write(bytes(json_str, "utf-8"))
Example #6
Source File: idcard_recognize.py From idcardocr with GNU General Public License v3.0 | 6 votes |
def do_POST(self): #content_length = int(self.headers['Content-Length']) # <--- Gets the size of data # post_data = self.rfile.read(content_length) # <--- Gets the data itself ctype, pdict = cgi.parse_header(self.headers['content-type']) print(pdict) pdict['boundary'] = bytes(pdict['boundary'], "utf-8") multipart_data = cgi.parse_multipart(self.rfile, pdict) filename = uuid.uuid1() fo = open("tmp/%s.jpg"%filename, "wb") # print(str(multipart_data)) # print(multipart_data.get('pic')[0]) fo.write( multipart_data.get('pic')[0] ) fo.close() result = process("tmp/%s.jpg"%filename) #print result self._set_headers() self.send_header("Content-Length", str(len(json.dumps(result).encode('utf-8')))) self.end_headers() self.wfile.write(json.dumps(result).encode('utf-8'))
Example #7
Source File: test_client_attachments.py From python-asana with MIT License | 5 votes |
def test_attachments_create_on_task(self): res = { "data": { "id": 5678, "name": "file.txt" } } responses.add(POST, 'http://app/tasks/1337/attachments', status=200, body=json.dumps(res), match_querystring=True) self.assertEqual(self.client.attachments.create_on_task(1337, 'file content', 'file name', 'file content-type'), res['data']) request_content_type, pdict = cgi.parse_header(responses.calls[0].request.headers['Content-Type']) self.assertEqual(request_content_type, 'multipart/form-data') content_file = six.BytesIO(responses.calls[0].request.body) multipart = cgi.parse_multipart(content_file, { 'boundary': six.b(pdict['boundary']) }) self.assertEqual(multipart['file'][0], six.b('file content')) # TODO: verify filename and content-type, possibly using a different multipart decoder
Example #8
Source File: config_server.py From dreampi with MIT License | 5 votes |
def _get_post_data(self): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': postvars = cgi.parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': length = int(self.headers.getheader('content-length')) postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1) else: postvars = {} return postvars
Example #9
Source File: utils.py From django-cas-server with GNU General Public License v3.0 | 5 votes |
def do_POST(self): """Called on a POST request on the BaseHTTPServer""" ctype, pdict = cgi.parse_header(self.headers.get('content-type')) if ctype == 'multipart/form-data': postvars = cgi.parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': length = int(self.headers.get('content-length')) postvars = parse_qs(self.rfile.read(length), keep_blank_values=1) else: postvars = {} self.server.PARAMS = postvars
Example #10
Source File: firemisp.py From FireMISP with MIT License | 5 votes |
def _parse_POST(self): import cgi ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': postvars = cgi.parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': length = int(self.headers.getheader('content-length')) postvars = cgi.parse_qs( self.rfile.read(length), keep_blank_values=1) else: postvars = {} return postvars # -------------- POST handler: where the magic happens --------------
Example #11
Source File: SimpleAsyncHTTPServer.py From cldstk-deploy with MIT License | 5 votes |
def do_POST(self): """Begins serving a POST request. The request data must be readable on a file-like object called self.rfile""" ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) length = int(self.headers.getheader('content-length')) if ctype == 'multipart/form-data': self.body = cgi.parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': qs = self.rfile.read(length) self.body = cgi.parse_qs(qs, keep_blank_values=1) else: self.body = '' # Unknown content-type #self.handle_post_body() self.handle_data()
Example #12
Source File: basehandler.py From dcos with Apache License 2.0 | 5 votes |
def _parse_request_body(self): """Parse request body in order to extract arguments. This method recognizes both `multipart/form-data` and `application/x-www-form-urlencoded` encoded data. So the client can check how the tested Nginx behaves with different kinds of params. It's based on: http://stackoverflow.com/a/4233452 Returns: It returns a dictionary that contains all the parsed data. In case when body did not contain any arguments - an empty dict is returned. """ if 'content-type' not in self.headers: return {} ctype, pdict = parse_header(self.headers['content-type']) if ctype == 'multipart/form-data': postvars = parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': # This should work (TM) basing on HTML5 spec: # Which default character encoding to use can only be determined # on a case-by-case basis, but generally the best character # encoding to use as a default is the one that was used to # encode the page on which the form used to create the payload # was itself found. In the absence of a better default, # UTF-8 is suggested. length = int(self.headers['content-length']) post_data = self.rfile.read(length).decode('utf-8') postvars = parse_qs(post_data, keep_blank_values=1, encoding="utf-8", errors="strict", ) else: postvars = {} return postvars
Example #13
Source File: test_cgi.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_parse_multipart(self): fp = BytesIO(POSTDATA.encode('latin1')) env = {'boundary': BOUNDARY.encode('latin1'), 'CONTENT-LENGTH': '558'} result = cgi.parse_multipart(fp, env) expected = {'submit': [b' Add '], 'id': [b'1234'], 'file': [b'Testing 123.\n'], 'title': [b'']} self.assertEqual(result, expected)
Example #14
Source File: connection.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def parse_multipart_files(request): '''Given a prepared reqest, return a file-like object containing the original data. This is pretty hacky.''' # Start by grabbing the pdict. _, pdict = cgi.parse_header(request.headers['Content-Type']) # Now, wrap the multipart data in a BytesIO buffer. This is annoying. buf = BytesIO() buf.write(request.body) buf.seek(0) # Parse the data. Simply take the first file. data = cgi.parse_multipart(buf, pdict) _, filedata = data.popitem() buf.close() # Get a BytesIO now, and write the file into it. buf = BytesIO() buf.write(''.join(filedata)) buf.seek(0) return buf # Taken from urllib3 (actually # https://github.com/shazow/urllib3/pull/394). Once it is fully upstreamed to # requests.packages.urllib3 we can just use that.
Example #15
Source File: server.py From TwinGAN with Apache License 2.0 | 5 votes |
def parse_POST(self): ctype, pdict = parse_header(self.headers['content-type']) pdict['boundary'] = str(pdict['boundary']).encode('utf-8') if ctype == 'multipart/form-data': postvars = parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': length = int(self.headers['content-length']) postvars = parse_qs( self.rfile.read(length), keep_blank_values=1) else: postvars = {} return postvars
Example #16
Source File: handlers.py From schemathesis with MIT License | 5 votes |
def _decode_multipart(content: bytes, content_type: str) -> Dict[str, str]: # a simplified version of multipart encoding that satisfies testing purposes _, options = cgi.parse_header(content_type) options["boundary"] = options["boundary"].encode() options["CONTENT-LENGTH"] = len(content) return {key: value[0].decode() for key, value in cgi.parse_multipart(io.BytesIO(content), options).items()}
Example #17
Source File: test_cgi.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_parse_multipart(self): fp = BytesIO(POSTDATA.encode('latin1')) env = {'boundary': BOUNDARY.encode('latin1'), 'CONTENT-LENGTH': '558'} result = cgi.parse_multipart(fp, env) expected = {'submit': [b' Add '], 'id': [b'1234'], 'file': [b'Testing 123.\n'], 'title': [b'']} self.assertEqual(result, expected)
Example #18
Source File: interactive.py From neural_chat with MIT License | 5 votes |
def do_POST(self): """Handle POST.""" if self.path != '/interact': return self.respond({'status': 500}) ctype, pdict = cgi.parse_header(self.headers['content-type']) pdict['boundary'] = bytes(pdict['boundary'], "utf-8") postvars = cgi.parse_multipart(self.rfile, pdict) model_response = self.interactive_running(postvars) self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() json_str = json.dumps(model_response) self.wfile.write(bytes(json_str, 'utf-8'))
Example #19
Source File: HTTPRequestHandler.py From honeything with GNU General Public License v3.0 | 5 votes |
def do_POST(self): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) length = int(self.headers.getheader('content-length')) if ctype == 'multipart/form-data': postvars = cgi.parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1) else: postvars = {} if self.path == '/Forms/login_security_1.html': auth = Auth() if auth.http_client_auth(postvars): credentials = auth.get_credentials() self.send_response(303) self.send_header('Location', '/rpSys.html') self.send_header('Set-Cookie', 'C0=' + credentials['user'] + '; path=/') self.send_header('Set-Cookie', 'C1=' + credentials['pass'] + '; path=/') self.end_headers() self.log_http(303, postvars) else: self.do_GET() self.log_http(200, postvars) else: self.send_response(200) self.send_header('Content-Type', 'text/html') self.end_headers() self.log_http(200, postvars)
Example #20
Source File: http.py From learn_python3_spider with MIT License | 5 votes |
def _parseHeader(line): # cgi.parse_header requires a str key, pdict = cgi.parse_header(line.decode('charmap')) # We want the key as bytes, and cgi.parse_multipart (which consumes # pdict) expects a dict of str keys but bytes values key = key.encode('charmap') pdict = {x:y.encode('charmap') for x, y in pdict.items()} return (key, pdict)
Example #21
Source File: handler.py From koadic with Apache License 2.0 | 5 votes |
def parse_post_vars(self): ctype, pdict = cgi.parse_header(self.headers['content-type']) if ctype == 'multipart/form-data': postvars = cgi.parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': length = int(self.headers['content-length']) postvars = parse_qs(self.rfile.read(length), keep_blank_values=1) else: postvars = {} return postvars
Example #22
Source File: test_cgi.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_parse_multipart(self): fp = BytesIO(POSTDATA.encode('latin1')) env = {'boundary': BOUNDARY.encode('latin1'), 'CONTENT-LENGTH': '558'} result = cgi.parse_multipart(fp, env) expected = {'submit': [b' Add '], 'id': [b'1234'], 'file': [b'Testing 123.\n'], 'title': [b'']} self.assertEqual(result, expected)
Example #23
Source File: http.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _parseHeader(line): # cgi.parse_header requires a str key, pdict = cgi.parse_header(line.decode('charmap')) # We want the key as bytes, and cgi.parse_multipart (which consumes # pdict) expects a dict of str keys but bytes values key = key.encode('charmap') pdict = {x:y.encode('charmap') for x, y in pdict.items()} return (key, pdict)
Example #24
Source File: tests.py From anytask with MIT License | 5 votes |
def do_POST(self): # NOQA content_type, pdict = cgi.parse_header(self.headers.getheader("content-type")) fields = cgi.parse_multipart(self.rfile, pdict) if self.path.startswith("/anytask/submit"): if "_failed_" in fields["file"][0]: reply = { 'error': { 'message': "Submit error in fake server!" } } else: reply = { 'result': { 'value': "1", } } self.send_response(200) self.end_headers() json.dump(reply, self.wfile) return self.send_response(501) self.end_headers()
Example #25
Source File: http.py From python-for-android with Apache License 2.0 | 4 votes |
def requestReceived(self, command, path, version): """ Called by channel when all data has been received. This method is not intended for users. @type command: C{str} @param command: The HTTP verb of this request. This has the case supplied by the client (eg, it maybe "get" rather than "GET"). @type path: C{str} @param path: The URI of this request. @type version: C{str} @param version: The HTTP version of this request. """ self.content.seek(0,0) self.args = {} self.stack = [] self.method, self.uri = command, path self.clientproto = version x = self.uri.split('?', 1) if len(x) == 1: self.path = self.uri else: self.path, argstring = x self.args = parse_qs(argstring, 1) # cache the client and server information, we'll need this later to be # serialized and sent with the request so CGIs will work remotely self.client = self.channel.transport.getPeer() self.host = self.channel.transport.getHost() # Argument processing args = self.args ctype = self.requestHeaders.getRawHeaders('content-type') if ctype is not None: ctype = ctype[0] if self.method == "POST" and ctype: mfd = 'multipart/form-data' key, pdict = cgi.parse_header(ctype) if key == 'application/x-www-form-urlencoded': args.update(parse_qs(self.content.read(), 1)) elif key == mfd: try: args.update(cgi.parse_multipart(self.content, pdict)) except KeyError, e: if e.args[0] == 'content-disposition': # Parse_multipart can't cope with missing # content-dispostion headers in multipart/form-data # parts, so we catch the exception and tell the client # it was a bad request. self.channel.transport.write( "HTTP/1.1 400 Bad Request\r\n\r\n") self.channel.transport.loseConnection() return raise self.content.seek(0, 0)
Example #26
Source File: http.py From BitTorrent with GNU General Public License v3.0 | 4 votes |
def requestReceived(self, command, path, version): """Called by channel when all data has been received. This method is not intended for users. """ self.content.seek(0,0) self.args = {} self.stack = [] self.method, self.uri = command, path self.clientproto = version x = self.uri.split('?') if len(x) == 1: self.path = self.uri else: if len(x) != 2: log.msg("May ignore parts of this invalid URI: %s" % repr(self.uri)) self.path, argstring = x[0], x[1] self.args = parse_qs(argstring, 1) # cache the client and server information, we'll need this later to be # serialized and sent with the request so CGIs will work remotely self.client = self.channel.transport.getPeer() self.host = self.channel.transport.getHost() # Argument processing args = self.args ctype = self.getHeader('content-type') if self.method == "POST" and ctype: mfd = 'multipart/form-data' key, pdict = cgi.parse_header(ctype) if key == 'application/x-www-form-urlencoded': args.update(parse_qs(self.content.read(), 1)) elif key == mfd: try: args.update(cgi.parse_multipart(self.content, pdict)) except KeyError, e: if e.args[0] == 'content-disposition': # Parse_multipart can't cope with missing # content-dispostion headers in multipart/form-data # parts, so we catch the exception and tell the client # it was a bad request. self.channel.transport.write( "HTTP/1.1 400 Bad Request\r\n\r\n") self.channel.transport.loseConnection() return raise