当前位置: 首页>>代码示例>>Python>>正文


Python mimetools.choose_boundary方法代码示例

本文整理汇总了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) 
开发者ID:glmcdona,项目名称:meddle,代码行数:19,代码来源:MimeWriter.py

示例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 
开发者ID:Esri,项目名称:portalpy,代码行数:21,代码来源:portalpy.py

示例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 
开发者ID:tuwid,项目名称:darkc0de-old-stuff,代码行数:23,代码来源:multipartpost.py

示例4: __init__

# 需要导入模块: import mimetools [as 别名]
# 或者: from mimetools import choose_boundary [as 别名]
def __init__(self):
        self.form_fields = []
        self.files = []
        self.boundary = mimetools.choose_boundary() 
开发者ID:ladybug-tools,项目名称:honeybee,代码行数:6,代码来源:mpupload.py

示例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 
开发者ID:krintoxi,项目名称:NoobSec-Toolkit,代码行数:35,代码来源:multipartpost.py

示例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 
开发者ID:cbrgm,项目名称:telegram-robot-rss,代码行数:34,代码来源:inputfile.py

示例7: __init__

# 需要导入模块: import mimetools [as 别名]
# 或者: from mimetools import choose_boundary [as 别名]
def __init__(self):
        self.files = []
        self.boundary = mimetools.choose_boundary() 
开发者ID:CalthorpeAnalytics,项目名称:urbanfootprint,代码行数:5,代码来源:arc_tool.py

示例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) 
开发者ID:alfa-addon,项目名称:addon,代码行数:39,代码来源:net.py

示例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 
开发者ID:alfa-addon,项目名称:addon,代码行数:28,代码来源:MultipartPostHandler.py

示例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) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:8,代码来源:test_mimetools.py

示例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 
开发者ID:evilbinary,项目名称:robot,代码行数:8,代码来源:tornadohttpclient.py

示例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 
开发者ID:sabri-zaki,项目名称:EasY_HaCk,代码行数:35,代码来源:multipartpost.py


注:本文中的mimetools.choose_boundary方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。