本文整理匯總了Python中mimetools.choose_boundary方法的典型用法代碼示例。如果您正苦於以下問題:Python mimetools.choose_boundary方法的具體用法?Python mimetools.choose_boundary怎麽用?Python mimetools.choose_boundary使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mimetools
的用法示例。
在下文中一共展示了mimetools.choose_boundary方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: startmultipartbody
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def startmultipartbody(self, subtype, boundary=None, plist=[], prefix=1):
"""Returns a file-like object for writing the body of the message.
Additionally, this method initializes the multi-part code, where the
subtype parameter provides the multipart subtype, the boundary
parameter may provide a user-defined boundary specification, and the
plist parameter provides optional parameters for the subtype. The
optional argument, prefix, determines where the header is inserted;
0 means append at the end, 1 means insert at the start. The default
is to insert at the start. Subparts should be created using the
nextpart() method.
"""
self._boundary = boundary or mimetools.choose_boundary()
return self.startbody("multipart/" + subtype,
[("boundary", self._boundary)] + plist,
prefix=prefix)
示例2: _encode_multipart_formdata
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def _encode_multipart_formdata(self, fields, files):
boundary = mimetools.choose_boundary()
buf = StringIO()
for (key, value) in fields.iteritems():
buf.write('--%s\r\n' % boundary)
buf.write('Content-Disposition: form-data; name="%s"' % key)
buf.write('\r\n\r\n' + _tostr(value) + '\r\n')
for (key, filepath, filename) in files:
buf.write('--%s\r\n' % boundary)
buf.write('Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename))
buf.write('Content-Type: %s\r\n' % (self._get_content_type(filename)))
f = open(filepath, "rb")
try:
buf.write('\r\n' + f.read() + '\r\n')
finally:
f.close()
buf.write('--' + boundary + '--\r\n\r\n')
buf = buf.getvalue()
return boundary, buf
示例3: multipart_encode
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def multipart_encode(vars, files, boundary = None, buffer = None):
if boundary is None:
boundary = mimetools.choose_boundary()
if buffer is None:
buffer = ''
for(key, value) in vars:
buffer += '--%s\r\n' % boundary
buffer += 'Content-Disposition: form-data; name="%s"' % key
buffer += '\r\n\r\n' + value + '\r\n'
for(key, fd) in files:
file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
filename = fd.name.split('/')[-1]
contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
buffer += '--%s\r\n' % boundary
buffer += 'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename)
buffer += 'Content-Type: %s\r\n' % contenttype
# buffer += 'Content-Length: %s\r\n' % file_size
fd.seek(0)
buffer += '\r\n' + fd.read() + '\r\n'
buffer += '--%s--\r\n\r\n' % boundary
return boundary, buffer
示例4: __init__
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def __init__(self):
self.form_fields = []
self.files = []
self.boundary = mimetools.choose_boundary()
示例5: multipart_encode
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def multipart_encode(vars, files, boundary=None, buf=None):
if boundary is None:
boundary = mimetools.choose_boundary()
if buf is None:
buf = ''
for (key, value) in vars:
if key is not None and value is not None:
buf += '--%s\r\n' % boundary
buf += 'Content-Disposition: form-data; name="%s"' % key
buf += '\r\n\r\n' + value + '\r\n'
for (key, fd) in files:
file_size = os.fstat(fd.fileno())[stat.ST_SIZE] if isinstance(fd, file) else fd.len
filename = fd.name.split('/')[-1] if '/' in fd.name else fd.name.split('\\')[-1]
try:
contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
except:
# Reference: http://bugs.python.org/issue9291
contenttype = 'application/octet-stream'
buf += '--%s\r\n' % boundary
buf += 'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename)
buf += 'Content-Type: %s\r\n' % contenttype
# buf += 'Content-Length: %s\r\n' % file_size
fd.seek(0)
buf = str(buf) if not isinstance(buf, unicode) else buf.encode("utf8")
buf += '\r\n%s\r\n' % fd.read()
buf += '--%s--\r\n\r\n' % boundary
return boundary, buf
示例6: __init__
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
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
示例7: __init__
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def __init__(self):
self.files = []
self.boundary = mimetools.choose_boundary()
示例8: _upload
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def _upload(self, upload, params):
import mimetools
import itertools
res = []
boundary = mimetools.choose_boundary()
part_boundary = '--' + boundary
if params:
for name, value in params.items():
res.append([part_boundary, 'Content-Disposition: form-data; name="%s"' % name, '', value])
if isinstance(upload, dict):
upload = [upload]
for obj in upload:
name = obj.get('name')
filename = obj.get('filename', 'default')
content_type = obj.get('content-type')
try:
body = obj['body'].read()
except AttributeError:
body = obj['body']
if content_type:
res.append([part_boundary,
'Content-Disposition: file; name="%s"; filename="%s"' % (name, urllib.parse.quote(filename)),
'Content-Type: %s' % content_type, '', body])
else:
res.append([part_boundary,
'Content-Disposition: file; name="%s"; filename="%s"' % (name, urllib.parse.quote(filename)), '',
body])
result = list(itertools.chain(*res))
result.append('--' + boundary + '--')
result.append('')
return boundary, '\r\n'.join(result)
示例9: multipart_encode
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def multipart_encode(vars, files, boundary = None, buffer = None):
if boundary is None:
boundary = mimetools.choose_boundary()
if buffer is None:
buffer = StringIO()
for(key, value) in vars:
buffer.write('--%s\r\n' % boundary)
buffer.write('Content-Disposition: form-data; name="%s"' % key)
if value is None:
value = ""
# if type(value) is not str, we need str(value) to not error with cannot concatenate 'str'
# and 'dict' or 'tuple' or somethingelse objects
buffer.write('\r\n\r\n' + str(value) + '\r\n')
for(key, fd) in files:
file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
filename = fd.name.split('/')[-1]
contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
buffer.write('--%s\r\n' % boundary)
buffer.write('Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename))
buffer.write('Content-Type: %s\r\n' % contenttype)
buffer.write('Content-Length: %s\r\n' % file_size)
fd.seek(0)
buffer.write('\r\n' + fd.read() + '\r\n')
buffer.write('--' + boundary + '--\r\n')
buffer = buffer.getvalue()
return boundary, buffer
示例10: test_boundary
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def test_boundary(self):
s = set([""])
for i in xrange(100):
nb = mimetools.choose_boundary()
self.assert_(nb not in s)
s.add(nb)
示例11: __init__
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def __init__(self):
self.form_fields = []
self.files = []
self.boundary = mimetools.choose_boundary()
self.content_type = 'multipart/form-data; boundary=%s' % self.boundary
return
示例12: multipart_encode
# 需要導入模塊: import mimetools [as 別名]
# 或者: from mimetools import choose_boundary [as 別名]
def multipart_encode(vars, files, boundary=None, buf=None):
if boundary is None:
boundary = mimetools.choose_boundary()
if buf is None:
buf = ""
for (key, value) in vars:
if key is not None and value is not None:
buf += "--%s\r\n" % boundary
buf += "Content-Disposition: form-data; name=\"%s\"" % key
buf += "\r\n\r\n" + value + "\r\n"
for (key, fd) in files:
file_size = os.fstat(fd.fileno())[stat.ST_SIZE] if isinstance(fd, file) else fd.len
filename = fd.name.split("/")[-1] if "/" in fd.name else fd.name.split("\\")[-1]
try:
contenttype = mimetypes.guess_type(filename)[0] or "application/octet-stream"
except:
# Reference: http://bugs.python.org/issue9291
contenttype = "application/octet-stream"
buf += "--%s\r\n" % boundary
buf += "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n" % (key, filename)
buf += "Content-Type: %s\r\n" % contenttype
# buf += "Content-Length: %s\r\n" % file_size
fd.seek(0)
buf = str(buf) if not isinstance(buf, unicode) else buf.encode("utf8")
buf += "\r\n%s\r\n" % fd.read()
buf += "--%s--\r\n\r\n" % boundary
return boundary, buf