linkahead.connection.encode module
multipart/form-data encoding module.
This module provides functions that faciliate encoding name/value pairs as multipart/form-data suitable for a HTTP POST or PUT request.
multipart/form-data is the standard way to upload files over HTTP
- class linkahead.connection.encode.MultipartParam(name: str, value=None, filename=None, filetype=None, filesize=None, fileobj=None, callback=None)
Bases:
objectRepresents a single parameter in a multipart/form-data request.
nameis the name of this parameter.If
valueis set, it must be a string or unicode object to use as the data for this parameter.If
filenameis set, it is what to say that this parameter’s filename is. Note that this does not have to be the actual filename any local file.If
filetypeis set, it is used as the Content-Type for this parameter. If unset it defaults to “text/plain; charset=utf8”If
filesizeis set, it specifies the length of the filefileobjIf
fileobjis set, it must be a file-like object that supports .read().Both
valueandfileobjmust not be set, doing so will raise a ValueError assertion.If
fileobjis set, andfilesizeis not specified, then the file’s size will be determined first by stat’ingfileobj’s file descriptor, and if that fails, by seeking to the end of the file, recording the current position as the size, and then by seeking back to the beginning of the file.callbackis a callable which will be called from iter_encode with (self, current_transferred, total), representing the current parameter, current amount transferred, and the total size.- encode(boundary)
Returns the string encoding of this parameter.
- encode_hdr(boundary)
Returns the header of the encoding of this parameter.
- classmethod from_file(paramname, filename)
Returns a new MultipartParam object constructed from the local file at
filename.filesizeis determined by os.path.getsize(filename)filetypeis determined by mimetypes.guess_type(filename)[0]filenameis set to os.path.basename(filename)
- classmethod from_params(params)
Returns a list of MultipartParam objects from a sequence of name, value pairs, MultipartParam instances, or from a mapping of names to values.
The values may be strings or file objects, or MultipartParam objects. MultipartParam object names must match the given names in the name,value pairs or mapping, if applicable.
- get_size(boundary)
Returns the size in bytes that this param will be when encoded with the given boundary.
- iter_encode(boundary, blocksize=4096)
Yields the encoding of this parameter If self.fileobj is set, then blocks of
blocksizebytes are read and yielded.
- reset()
Reset the file object’s read pointer.
- class linkahead.connection.encode.ReadableMultiparts(multipart_yielder)
Bases:
objectWraps instances of the MultipartYielder class as a readable and withable object.
- close()
- read(size=-1)
- linkahead.connection.encode.encode_and_quote(data: str | None) str | None
If
datais unicode, return urllib.quote_plus(data.encode(“utf-8”)) otherwise return urllib.quote_plus(data)
- linkahead.connection.encode.encode_file_header(boundary, paramname, filesize, filename=None, filetype=None)
Returns the leading data for a multipart/form-data field that contains file data.
boundaryis the boundary string used throughout a single request to separate variables.paramnameis the name of the variable in this request.filesizeis the size of the file data.filenameif specified is the filename to give to this field. This field is only useful to the server for determining the original filename.filetypeif specified is the MIME type of this file.The actual file data should be sent after this header has been sent.
- linkahead.connection.encode.encode_string(boundary, name, value)
Returns
nameandvalueencoded as a multipart/form-data variable.boundaryis the boundary string used throughout a single request to separate variables.
- linkahead.connection.encode.gen_boundary()
Returns a random string to use as the boundary for a message.
- linkahead.connection.encode.get_body_size(params, boundary)
Returns the number of bytes that the multipart/form-data encoding of
paramswill be.
- linkahead.connection.encode.get_headers(params, boundary)
Returns a dictionary with Content-Type and Content-Length headers for the multipart/form-data encoding of
params.
- linkahead.connection.encode.multipart_encode(params, boundary=None, callback=None)
Encode
paramsas multipart/form-data.paramsshould be a sequence of (name, value) pairs or MultipartParam objects, or a mapping of names to values. Values are either strings parameter values, or file-like objects to use as the parameter value. The file-like objects must support .read() and either .fileno() or both .seek() and .tell().If
boundaryis set, then it as used as the MIME boundary. Otherwise a randomly generated boundary will be used. In either case, if the boundary string appears in the parameter values a ValueError will be raised.If
callbackis set, it should be a callback which will get called as blocks of data are encoded. It will be called with (param, current_transferred, total), indicating the current parameter being encoded, the current amount encoded, and the total amount to encode.Returns a tuple of datagen, headers, where datagen is a generator that will yield blocks of data that make up the encoded parameters, and headers is a dictionary with the assoicated Content-Type and Content-Length headers.
Examples:
>>> datagen, headers = multipart_encode( [("key", "value1"), ("key", "value2")] ) >>> s = "".join(datagen) >>> assert "value2" in s and "value1" in s
>>> p = MultipartParam("key", "value2") >>> datagen, headers = multipart_encode( [("key", "value1"), p] ) >>> s = "".join(datagen) >>> assert "value2" in s and "value1" in s
>>> datagen, headers = multipart_encode( {"key": "value1"} ) >>> s = "".join(datagen) >>> assert "value2" not in s and "value1" in s