Python email.generator._make_boundary() Examples
The following are 2
code examples of email.generator._make_boundary().
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
email.generator
, or try the search function
.
Example #1
Source File: inputfile.py From telegram-robot-rss with Mozilla Public License 2.0 | 5 votes |
def __init__(self, data): self.data = data self.boundary = choose_boundary() for t in FILE_TYPES: if t in data: self.input_name = t self.input_file = data.pop(t) break else: raise TelegramError('Unknown inputfile type') if hasattr(self.input_file, 'read'): self.filename = None self.input_file_content = self.input_file.read() if 'filename' in data: self.filename = self.data.pop('filename') elif hasattr(self.input_file, 'name'): # on py2.7, pylint fails to understand this properly # pylint: disable=E1101 self.filename = os.path.basename(self.input_file.name) try: self.mimetype = self.is_image(self.input_file_content) if not self.filename or '.' not in self.filename: self.filename = self.mimetype.replace('/', '.') except TelegramError: if self.filename: self.mimetype = mimetypes.guess_type( self.filename)[0] or DEFAULT_MIME_TYPE else: self.mimetype = DEFAULT_MIME_TYPE
Example #2
Source File: scout_client.py From scout with MIT License | 4 votes |
def post_files(self, url, json_data, files=None): if not files or not isinstance(files, dict): raise ValueError('One or more files is required. Files should be ' 'passed as a dictionary of filename: file-like-' 'object.') boundary = choose_boundary() form_files = [] for i, (filename, file_obj) in enumerate(files.items()): try: data = file_obj.read() except AttributeError: data = bytes(file_obj) mimetype = mimetypes.guess_type(filename)[0] form_files.append(( 'file_%s' % i, filename, mimetype or 'application/octet-stream', data)) part_boundary = '--' + boundary parts = [ part_boundary, 'Content-Disposition: form-data; name="data"', '', json.dumps(json_data)] for field_name, filename, mimetype, data in form_files: parts.extend(( part_boundary, 'Content-Disposition: file; name="%s"; filename="%s"' % ( field_name, filename), 'Content-Type: %s' % mimetype, '', data)) parts.append('--' + boundary + '--') parts.append('') headers = {'Content-Type': 'multipart/form-data; boundary=%s' % boundary} if self.key: headers['key'] = self.key data = '\r\n'.join(parts) if not isinstance(data, bytes): data = data.encode('utf-8') request = Request(self.get_full_url(url), data=data, headers=headers) return json.loads(urlopen(request).read())