How to use _iter_multipart_parts method in localstack

Best Python code snippet using localstack_python

multipart_content.py

Source:multipart_content.py Github

copy

Full Screen

1import cgi2import email.parser3from localstack.utils.common import to_bytes4def _iter_multipart_parts(some_bytes, boundary):5 """ Generate a stream of dicts and bytes for each message part.6 Content-Disposition is used as a header for a multipart body:7 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition8 """9 try:10 parse_data = email.parser.BytesHeaderParser().parsebytes11 except AttributeError:12 # Fall back in case of Python 2.x13 parse_data = email.parser.HeaderParser().parsestr14 while True:15 try:16 part, some_bytes = some_bytes.split(boundary, 1)17 except ValueError:18 # Ran off the end, stop.19 break20 if b'\r\n\r\n' not in part:21 # Real parts have headers and a value separated by '\r\n'.22 continue23 part_head, _ = part.split(b'\r\n\r\n', 1)24 head_parsed = parse_data(part_head.lstrip(b'\r\n'))25 if 'Content-Disposition' in head_parsed:26 _, params = cgi.parse_header(head_parsed['Content-Disposition'])27 yield params, part28def expand_multipart_filename(data, headers):29 """ Replace instance of '${filename}' in key with given file name.30 Data is given as multipart form submission bytes, and file name is31 replace according to Amazon S3 documentation for Post uploads:32 http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html33 """34 _, params = cgi.parse_header(headers.get('Content-Type', ''))35 if 'boundary' not in params:36 return data37 boundary = params['boundary'].encode('ascii')38 data_bytes = to_bytes(data)39 filename = None40 for (disposition, _) in _iter_multipart_parts(data_bytes, boundary):41 if disposition.get('name') == 'file' and 'filename' in disposition:42 filename = disposition['filename']43 break44 if filename is None:45 # Found nothing, return unaltered46 return data47 for (disposition, part) in _iter_multipart_parts(data_bytes, boundary):48 if disposition.get('name') == 'key' and b'${filename}' in part:49 search = boundary + part50 replace = boundary + part.replace(b'${filename}', filename.encode('utf8'))51 if search in data_bytes:52 return data_bytes.replace(search, replace)53 return data54def find_multipart_redirect_url(data, headers):55 """ Return object key and redirect URL if they can be found.56 Data is given as multipart form submission bytes, and redirect is found57 in the success_action_redirect field according to Amazon S358 documentation for Post uploads:59 http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html60 """61 _, params = cgi.parse_header(headers.get('Content-Type', ''))62 key, redirect_url = None, None63 if 'boundary' not in params:64 return key, redirect_url65 boundary = params['boundary'].encode('ascii')66 data_bytes = to_bytes(data)67 for (disposition, part) in _iter_multipart_parts(data_bytes, boundary):68 if disposition.get('name') == 'key':69 _, value = part.split(b'\r\n\r\n', 1)70 key = value.rstrip(b'\r\n--').decode('utf8')71 if key:72 for (disposition, part) in _iter_multipart_parts(data_bytes, boundary):73 if disposition.get('name') == 'success_action_redirect':74 _, value = part.split(b'\r\n\r\n', 1)75 redirect_url = value.rstrip(b'\r\n--').decode('utf8')...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful