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


Python mimetools.choose_boundary函数代码示例

本文整理汇总了Python中mimetools.choose_boundary函数的典型用法代码示例。如果您正苦于以下问题:Python choose_boundary函数的具体用法?Python choose_boundary怎么用?Python choose_boundary使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了choose_boundary函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: multipart_encode

 def multipart_encode(vars, files, boundary=None, buffer=None):
     if boundary is None:
         # Before :
         # boundary = mimetools.choose_boundary()
         # '127.0.0.1.1000.6267.1173556103.828.1'
         # This contains my IP address, I dont like that...
         # Now:
         m = hashlib.md5()
         m.update(mimetools.choose_boundary())
         boundary = m.hexdigest()
     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:
         filename = fd.name.split( os.path.sep )[-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' % getFileSize(fd)
         fd.seek(0)
         buffer += '\r\n' + fd.read() + '\r\n'
     buffer += '--%s--\r\n\r\n' % boundary
     return boundary, buffer
开发者ID:1d3df9903ad,项目名称:w3af,代码行数:29,代码来源:MultipartPostHandler.py

示例2: encode_multipart_formdata

def encode_multipart_formdata(fields, files):
    BOUNDARY = mimetools.choose_boundary()
    CRLF = '\r\n'
    L = []
    total_size = 0
    L = []
    for key, value in fields.items():
        key, value = key.encode('utf8'), value.encode('utf8')
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)

    for pair in files:
        key, filename = pair[0].encode('utf8'), pair[1].encode('utf8')
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' %
            (key, 'hotot.png'));
        L.append('Content-Type: %s' % get_content_type(filename))
        L.append('')
        L.append(file(filename).read())
        total_size += os.path.getsize(filename)

    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    headers = {'content-type':'multipart/form-data; boundary=%s' % BOUNDARY
        , 'content-length': str(len(body))};
    return headers, body
开发者ID:fma16,项目名称:Hotot,代码行数:29,代码来源:utils.py

示例3: multipart_encode

    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:

            # allow them to pass in a file or a tuple with name & data
            if type(fd) == file:
                name_in = fd.name
                fd.seek(0)
                data_in = fd.read()
            elif type(fd) in (tuple, list):
                name_in, data_in = fd

            filename = os.path.basename(name_in)
            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
            buffer += '\r\n' + data_in + '\r\n'
        buffer += '--%s--\r\n\r\n' % boundary
        return boundary, buffer
开发者ID:Akylas,项目名称:CouchPotatoServer,代码行数:28,代码来源:multipartpost.py

示例4: multipart_encode

    def multipart_encode(vars, files, boundary = None, buf = None):
        if boundary is None:
            boundary = mimetools.choose_boundary()
        if buf is None:
            buf = StringIO()
        for(key, value) in vars:
            buf.write('--%s\r\n' % boundary)
            buf.write('Content-Disposition: form-data; name="%s"' % key)
            buf.write('\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:][0]
            contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
            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' % contenttype)

            fd.seek(0)
            if compress:
                buf.write('\r\n' + zlib.compress(fd.read()) + '\r\n')
            else:
                buf.write('\r\n' + fd.read() + '\r\n')

        buf.write('--' + boundary + '--\r\n\r\n')
        buf = buf.getvalue()
        return boundary, buf
开发者ID:Big-Data,项目名称:pypes,代码行数:26,代码来源:FileCrawler.py

示例5: _upload

    def _upload(self, upload, params):
        res = []
        boundary = mimetools.choose_boundary()
        part_boundary = '--' + boundary

        if params:
            for name, value in params.iteritems():
                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.quote(filename)),
                            'Content-Type: %s' % content_type, '', body])
            else:
                res.append([part_boundary,
                            'Content-Disposition: file; name="%s"; filename="%s"' % (name, urllib.quote(filename)), '',
                            body])

        result = list(itertools.chain(*res))
        result.append('--' + boundary + '--')
        result.append('')
        return boundary, '\r\n'.join(result)
开发者ID:Inter95,项目名称:tutvguia,代码行数:34,代码来源:net.py

示例6: make_multipart_formdata

def make_multipart_formdata(values):
    boundary = mimetools.choose_boundary()
    content_type = 'multipart/form-data; boundary=' + boundary
    def parts():
        for field, value in values:
            field = str(field)
            yield '--' + boundary
            if isinstance(value, basestring):
                yield 'Content-Disposition: form-data; name="' + field + '"'
                yield ''
                yield str(value)
            else:
                filename, mimetype, file_ = value
                yield ('Content-Disposition: form-data; name="' + field +
                       '"; filename="' + str(filename) + '"')
                yield 'Content-Type: ' + str(mimetype)
                yield ''
                while 1:
                    chunk = file_.read(4096)
                    if chunk:
                        yield chunk
                    else:
                        break
        yield '--' + boundary + '--'
        yield ''
    return content_type, '\r\n'.join(parts())
开发者ID:dahlia,项目名称:github-distutils,代码行数:26,代码来源:github_distutils.py

示例7: encode_multipart_formdata

def encode_multipart_formdata(fields,files,mimetype=None):
    """
    Derived from - http://code.activestate.com/recipes/146306/

    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be 
    uploaded as files

    Returns (content_type, body) ready for httplib.HTTP instance
    """

    BOUNDARY = mimetools.choose_boundary()
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for (key, filename, value) in files:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (
                        key, filename))
        L.append('Content-Type: %s' % (mimetype or 
                                       mimetypes.guess_type(filename)[0] or 
                                       'application/octet-stream'))
        L.append('')
        L.append(value)
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(map(bytes,L))
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body
开发者ID:wesada,项目名称:icefilms-addon,代码行数:33,代码来源:minus.py

示例8: __init__

    def __init__(self,
                 data):
        self.data = data
        self.boundary = mimetools.choose_boundary()

        if 'audio' in data:
            self.input_name = 'audio'
            self.input_file = data.pop('audio')
        if 'document' in data:
            self.input_name = 'document'
            self.input_file = data.pop('document')
        if 'photo' in data:
            self.input_name = 'photo'
            self.input_file = data.pop('photo')
        if 'video' in data:
            self.input_name = 'video'
            self.input_file = data.pop('video')

        if isinstance(self.input_file, file):
            self.filename = os.path.basename(self.input_file.name)
            self.input_file_content = self.input_file.read()
        if 'http' in self.input_file:
            self.filename = os.path.basename(self.input_file)
            self.input_file_content = urllib2.urlopen(self.input_file).read()

        self.mimetype = mimetypes.guess_type(self.filename)[0] or \
            'application/octet-stream'
开发者ID:Avanatiker,项目名称:python-telegram-bot,代码行数:27,代码来源:inputfile.py

示例9: sendmail

def sendmail(From, To, Cc=None, Bcc=None, Subject="", 
			 Body="", File=None, Filename="", InReplyTo=None):
	text = makePart(Body, "quoted-printable")
	if File is not None:
	    data = File.read()
	    if data:
		attach = makePart(data, "base64")
		ext = os.path.splitext(Filename)[1]
		ctype = mimetypes.guess_type(ext)[0] or "application/octet-stream"
		attach["Content-Type"] = ctype
		Filename = os.path.split(Filename)[1]
		attach["Content-Disposition"] = 'attachment; filename="%s"' % Filename
		msg = Message(StringIO())
		msg.boundary = choose_boundary()
		msg["Content-Type"] = 'multipart/mixed; boundary="%s"' % msg.boundary
		msg.parts.extend([text, attach])
	    else: msg = text
	else: msg = text
	msg["From"] = From
	msg["To"] = To
	if Cc: msg["Cc"] = Cc
	if Bcc: msg["Bcc"] = Bcc
	if InReplyTo: msg["In-Reply-To"] = InReplyTo
	msg["Subject"] = Subject or ""
	msg["Mime-Version"] = "1.0"
	text["Content-Type"] = "text/plain; charset=ISO-8859-1"
	msg["X-Mailer"] = XMailer
	Recipients = msg.getaddrlist("to") + msg.getaddrlist("cc") + msg.getaddrlist("bcc")
	for i in range(len(Recipients)):
	    Recipients[i] = Recipients[i][1]
	return sendMessage(From, Recipients, msg)
开发者ID:MinasAbrahamyan,项目名称:bobomail,代码行数:31,代码来源:sendmail.py

示例10: encode_multipart_formdata

def encode_multipart_formdata(fields, files):
    BOUNDARY = mimetools.choose_boundary()
    CRLF = "\r\n"
    L = []
    total_size = 0
    L = []
    for key, value in fields.items():
        key, value = str(key).encode("utf8"), str(value).encode("utf8")
        L.append("--" + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append("")
        L.append(value)

    for pair in files:
        key, filename = pair[0].encode("utf8"), pair[1].encode("utf8")
        L.append("--" + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, "hotot.png"))
        L.append("Content-Type: %s" % get_content_type(filename))
        L.append("")
        L.append(file(filename).read())
        total_size += os.path.getsize(filename)

    L.append("--" + BOUNDARY + "--")
    L.append("")
    body = CRLF.join(L)
    headers = {"content-type": "multipart/form-data; boundary=%s" % BOUNDARY, "content-length": str(len(body))}
    return headers, body
开发者ID:sarim,项目名称:Hotot,代码行数:27,代码来源:utils.py

示例11: setup_multi_part_response

def setup_multi_part_response(r, rngs, clen, content_type):
    """ Setups a response object for a multi part response, based on the byte
    ranges requested. Based on Cherrypy 3.1 implementation.

    @param r: response object
    @param rngs: list of ranges
    @param clen: length of the body file
    """
    b = mimetools.choose_boundary()
    r.headers['Content-type'] = 'multipart/byteranges;'\
                                'boundary=%s' % b

    real_file = r.body

    def mpart_body_generator():
        yield '\r\n'

        for start, stop in rngs:
            yield '--%s\r\n' % b
            yield 'Content-type: %s\r\n' % content_type
            yield 'Content-range: bytes\r\n%s-%s/%s\r\n\r\n' % \
                  (start, stop - 1, clen)
            real_file.seek(start)
            for c in chunk_generator(real_file, chunks_size, stop-start):
                yield c
            yield '\r\n'
        yield '--%s--\r\n' % b

    r.body = mpart_body_generator()
开发者ID:AndyThirtover,项目名称:wb_gateway,代码行数:29,代码来源:webserver.py

示例12: http_request

    def http_request(self, request):
        data = request.get_data()
        if data is not None and not isinstance(data, str):
            files = []
            vars = []
            try:
                 for key, value in data.iteritems():
                     #XXX; better check
                     if hasattr(value, 'read'):
                         files.append((key, value))
                     else:
                         vars.append((key, value))
            except TypeError:
                systype, value, traceback = sys.exc_info()
                raise TypeError, "not a valid non-string sequence or mapping object", traceback

            if not files:
                data = urllib.urlencode(vars, doseq)
            else:
                boundary = mimetools.choose_boundary()
                data = encode_multipart(vars, files, boundary)
                contenttype = 'multipart/form-data; boundary=%s' % boundary
                if (request.has_header('Content-Type')
                    and request.get_header('Content-Type').find('multipart/form-data') != 0):
                    log.debug("replace Content-Type %s with %s %s" % (request.get_header('content-type'), 'multipart/form-data'))
                request.add_unredirected_header('Content-Type', contenttype)

            request.add_data(data)
        return request
开发者ID:fermat618,项目名称:pida,代码行数:29,代码来源:multipart.py

示例13: encode_multipart_formdata

def encode_multipart_formdata(fields, files):
    """
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return (content_type, body) ready for httplib.HTTP instance
    """
    mimetools._prefix = "some-random-string-you-like"    # vincent patch : http://mail.python.org/pipermail/python-list/2006-December/420360.html
    BOUNDARY = mimetools.choose_boundary()
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for (key, filename, value) in files:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        L.append('Content-Type: %s' % get_content_type(filename))
        L.append('')
        L.append(value)
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body
开发者ID:zy-sunshine,项目名称:pygtk-example,代码行数:26,代码来源:pycasaweb.py

示例14: POST

  def POST(self, path, parameter_dict, file_list=None):
    self._connect()
    parameter_dict.update(__ac_name=self.conn.username,
                          __ac_password=self.conn.password)
    header_dict = {'Content-type': "application/x-www-form-urlencoded"}
    if file_list is None:
      body = urllib.urlencode(parameter_dict)
    else:
      boundary = mimetools.choose_boundary()
      header_dict['Content-type'] = 'multipart/form-data; boundary=%s' % (
          boundary,)
      body = ''
      for k, v in parameter_dict.iteritems():
        body += '--%s\r\n' % boundary
        body += 'Content-Disposition: form-data; name="%s"\r\n' % k
        body += '\r\n'
        body += '%s\r\n' % v
      for name, filename in file_list:
        f = open(filename, 'r')
        body += '--%s\r\n' % boundary
        body += 'Content-Disposition: form-data; name="%s"; filename="%s"\r\n'\
                % (name, name)
        body += 'Content-Type: %s\r\n' % get_content_type(f)
        body += 'Content-Length: %d\r\n' % os.fstat(f.fileno())[stat.ST_SIZE]
        body += '\r\n'
        body += f.read()
        f.close()
        body += '\r\n'

    self.connection.request("POST", self.conn.path + '/' + path,
          body, header_dict)
    self.response = self.connection.getresponse()
开发者ID:desaintmartin,项目名称:slapos,代码行数:32,代码来源:erp5testreporthandler.py

示例15: multipart_encode

    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:
            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]
            filename = fd.name.split('/')[-1]
            contenttype = mimetypes.guess_type(filename)[0] or '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)
            buf += '\r\n%s\r\n' % fd.read()

        buf += '--%s--\r\n\r\n' % boundary

        return boundary, buf
开发者ID:DavisHevin,项目名称:sqli_benchmark,代码行数:28,代码来源:multipartpost.py


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