httpmultipart
The library is considered production ready.
#!usr/bin/env python
#coding: utf-8
import os
from pykit import httpmultipart
from pykit import fsutil
# http request headers
headers = {'Content-Length': 1200}
# http request fields
file_path = '/tmp/abc.txt'
fsutil.write_file(file_path, '123456789')
fields = [
{
'name': 'aaa',
'value': 'abcde',
},
{
'name': 'bbb',
'value': [open(file_path), os.path.getsize(file_path), 'abc.txt']
},
]
# get http request headers
multipart = httpmultipart.Multipart()
res_headers = multipart.make_headers(fields, headers=headers)
print res_headers
#output:
#{
# 'Content-Type': 'multipart/form-data; boundary=FormBoundaryrGKCBY7',
# 'Conetnt-Length': 1200,
#}
# get http request body reader
multipart = httpmultipart.Multipart()
body_reader = multipart.make_body_reader(fields)
data = []
for body in body_reader:
data.append(body)
print ''.join(data)
#output:
#--FormBoundaryrGKCBY7
#Content-Disposition: form-data; name=aaa
#
#abcde
#--FormBoundaryrGKCBY7
#Content-Disposition: form-data; name=bbb; filename=abc.txt
#Content-Type: text/plain
#
#123456789
#--FormBoundaryrGKCBY7--This module provides some util methods to get multipart headers and body.
syntax:
httpmultipart.MultipartError
The base class of the other exceptions in this module.
It is a subclass of Exception
syntax:
httpmultipart.InvalidArgumentTypeError
A subclass of MultipartError
Raise if the type of value is not a str or a list or the type of value[0]
is not a string, string reader, file reader or file object
syntax:
httpmultipart.Multipart.boundary
a placeholder that represents out specified delimiter
syntax:
httpmultipart.Multipart.block_size
It represents the size of each reading file
syntax:
httpmultipart.Multipart()
syntax:
httpmultipart.Multipart.make_headers(fields, headers=None)
Return a header according to the fields and headers
arguments:
-
fields: is a list of the dict, and each elements containsname,valueandheaders,headersis an optional argument-
name: It's a string that represents field's name -
value: The value represents field's content. The type of value can be a string or a list, string indicates that the field is a normal string, However, there are three arguments of list:content,sizeandfile_name-
content: The type ofcontentcan be string, reader, file object The string type refers to the user want to upload a string. It takes the string as the field bodyThe reader type refers to a generator. To read the contents of generator as the field body
The file object type refers to a file object, To read the contents of file as the field body
-
sizesizerefers to the length of the content, When the type ofcontentis a string, size can be None -
file_namefile_nameis an optional argument, iffile_nameis None, that indicates thatcontentis uploaded as a normal field, whereas, the field as a file
-
-
headers: a dict, key is thefield_header_name, value is thefield_header_value, it contains user defined headers and the required headers, such as 'Content-Disposition' and 'Content-Type'
-
-
headers: a dict of http request headers, key is theheader_name, value is theheader_value. It's a default argument and its default value is None
return: a dict that represents the request headers
syntax
httpmultipart.Multipart.make_body_reader(fields)
Return a body according to the fields
arguments:
fields: refer to the explanation above fields
return: a generator that represents the multipart request body
Ting Lv(吕婷) ting.lv@baishancloud.com
The MIT License (MIT)
Copyright (c) 2017 Ting Lv(吕婷) ting.lv@baishancloud.com