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 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))