Exemple #1
0
def multipart_encode(params, files, boundary=None, buf=None, sep=b'\r\n'):
	if buf is None: buf = []
	if boundary is None:
		boundary = Generator._make_boundary()
	for(key, value) in params:
		b = []
		b.append('--' + boundary)
		b.append('Content-Disposition: form-data; name="%s"' % key)
		b.append('')
		b.append(str(value))
		buf.extend(map(str.encode, b))
	for(key, fd) in files:
		if isinstance(fd, tuple):
			filename, data = fd
			try: fd = open(filename, 'rb')
			except: pass
		else:
			filename, data=os.path.basename(fd.name), None
		contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
		b = []
		b.append('--' + boundary)
		b.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
		b.append('Content-Type: ' + contenttype)
		b.append('')
		buf.extend(map(str.encode, b))
		if data is None:
			try: data = fd.read()
			except: data = b''
		buf.append(data)
	buf.append(('--%s--' % boundary).encode())
	buf.append(sep)
	return boundary, sep.join(buf)
Exemple #2
0
    def __init__(self, dsn_from, dsn_to, subject=None):

        self.context = {
            'boundary': Generator._make_boundary(),
            'from': dsn_from,
            'to': dsn_to,
            'subject': subject,
        }
Exemple #3
0
def multipart_encode(params, files, boundary=None, buf=None, sep=b'\r\n'):
    if buf is None: buf = []
    if boundary is None:
        boundary = Generator._make_boundary()
    for (key, value) in params:
        b = []
        b.append('--' + boundary)
        b.append('Content-Disposition: form-data; name="%s"' % key)
        b.append('')
        b.append(str(value))
        buf.extend(map(str.encode, b))
    for (key, fd) in files:
        if isinstance(fd, tuple):
            filename, data = fd
            try:
                fd = open(filename, 'rb')
            except:
                pass
        else:
            filename, data = os.path.basename(fd.name), None
        contenttype = mimetypes.guess_type(
            filename)[0] or 'application/octet-stream'
        b = []
        b.append('--' + boundary)
        b.append('Content-Disposition: form-data; name="%s"; filename="%s"' %
                 (key, filename))
        b.append('Content-Type: ' + contenttype)
        b.append('')
        buf.extend(map(str.encode, b))
        if data is None:
            try:
                data = fd.read()
            except:
                data = b''
        buf.append(data)
    buf.append(('--%s--' % boundary).encode())
    buf.append(sep)
    return boundary, sep.join(buf)