def __init__(self, name, value, fname=None, filetype=None, filesize=None): self.name = url_quote(name) if value is not None and not hasattr(value, "read"): value = url_quote(value) self.size = len(value) self.value = value if fname is not None: if isinstance(fname, unicode): fname = fname.encode("utf-8").encode("string_escape").replace('"', '\\"') else: fname = fname.encode("string_escape").replace('"', '\\"') self.fname = fname if filetype is not None: filetype = to_bytestring(filetype) self.filetype = filetype if isinstance(value, file) and filesize is None: try: value.flush() except IOError: pass self.size = int(os.fstat(value.fileno())[6])
def __init__(self, name, value, fname=None, filetype=None, filesize=None): self.name = url_quote(name) if value is not None and not hasattr(value, 'read'): value = url_quote(value) self.size = len(value) self.value = value if fname is not None: if isinstance(fname, unicode): fname = fname.encode("utf-8").encode("string_escape").replace( '"', '\\"') else: fname = fname.encode("string_escape").replace('"', '\\"') self.fname = fname if filetype is not None: filetype = to_bytestring(filetype) self.filetype = filetype if isinstance(value, file) and filesize is None: try: value.flush() except IOError: pass self.size = int(os.fstat(value.fileno())[6])
def encode_hdr(self, boundary): """Returns the header of the encoding of this parameter""" boundary = url_quote(boundary) headers = ["--%s" % boundary] if self.fname: disposition = 'form-data; name="%s"; filename="%s"' % (self.name, self.fname) else: disposition = 'form-data; name="%s"' % self.name headers.append("Content-Disposition: %s" % disposition) if self.filetype: filetype = self.filetype else: filetype = "text/plain; charset=utf-8" headers.append("Content-Type: %s" % filetype) headers.append("Content-Length: %i" % self.size) headers.append("") headers.append("") return "\r\n".join(headers)
def form_encode(obj, charser="utf8"): tmp = [] for key, value in obj.items(): tmp.append("%s=%s" % (url_quote(key), url_quote(value))) return to_bytestring("&".join(tmp))