Best Python code snippet using localstack_python
multipart_content.py
Source: multipart_content.py
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')...
Check out the latest blogs from LambdaTest on this topic:
The fact is not alien to us anymore that cross browser testing is imperative to enhance your application’s user experience. Enhanced knowledge of popular and highly acclaimed testing frameworks goes a long way in developing a new app. It holds more significance if you are a full-stack developer or expert programmer.
QA testers have a unique role and responsibility to serve the customer. Serving the customer in software testing means protecting customers from application defects, failures, and perceived failures from missing or misunderstood requirements. Testing for known requirements based on documentation or discussion is the core of the testing profession. One unique way QA testers can both differentiate themselves and be innovative occurs when senseshaping is used to improve the application user experience.
Having a good web design can empower business and make your brand stand out. According to a survey by Top Design Firms, 50% of users believe that website design is crucial to an organization’s overall brand. Therefore, businesses should prioritize website design to meet customer expectations and build their brand identity. Your website is the face of your business, so it’s important that it’s updated regularly as per the current web design trends.
Enterprise resource planning (ERP) is a form of business process management software—typically a suite of integrated applications—that assists a company in managing its operations, interpreting data, and automating various back-office processes. The introduction of a new ERP system is analogous to the introduction of a new product into the market. If the product is not handled appropriately, it will fail, resulting in significant losses for the business. Most significantly, the employees’ time, effort, and morale would suffer as a result of the procedure.
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!