我们从Python开源项目中,提取了以下47个代码示例,用于说明如何使用base64.encode()。
def encode(input, output, encoding): """Encode common content-transfer-encodings (base64, quopri, uuencode).""" if encoding == 'base64': import base64 return base64.encode(input, output) if encoding == 'quoted-printable': import quopri return quopri.encode(input, output, 0) if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'): import uu return uu.encode(input, output) if encoding in ('7bit', '8bit'): return output.write(input.read()) if encoding in encodetab: pipethrough(input, encodetab[encoding], output) else: raise ValueError, \ 'unknown Content-Transfer-Encoding: %s' % encoding # The following is no longer used for standard encodings # XXX This requires that uudecode and mmencode are in $PATH
def dump_struct(self, value, write, escape=escape): i = id(value) if i in self.memo: raise TypeError, "cannot marshal recursive dictionaries" self.memo[i] = None dump = self.__dump write("<value><struct>\n") for k, v in value.items(): write("<member>\n") if type(k) is not StringType: if unicode and type(k) is UnicodeType: k = k.encode(self.encoding) else: raise TypeError, "dictionary key must be string" write("<name>%s</name>\n" % escape(k)) dump(v, write) write("</member>\n") write("</struct></value>\n") del self.memo[i]
def send_content(self, connection, request_body): connection.putheader("Content-Type", "text/xml") #optionally encode the request if (self.encode_threshold is not None and self.encode_threshold < len(request_body) and gzip): connection.putheader("Content-Encoding", "gzip") request_body = gzip_encode(request_body) connection.putheader("Content-Length", str(len(request_body))) connection.endheaders(request_body) ## # Parse response. # # @param file Stream. # @return Response tuple and target method.
def __call__(self): access_token=json.loads(requests.post("https://openapi.baidu.com/oauth/2.0/token",data=self.key).content).get("access_token") c=open("c.txt","a+") with open("test1.mp3","rb") as f: base64.encode(f,c) vdata=c.read() c.close() data={ "format":"wav", "rate":8000, "channel":1, "token":access_token.encode('utf-8'), "cuid":"rui7157", "len":4096, "speech":vdata, } print data result=requests.post("http://vop.baidu.com/server_api",data=data).content print result
def dump_struct(self, value, write, escape=escape): i = id(value) if i in self.memo: raise TypeError, "cannot marshal recursive dictionaries" self.memo[i] = None dump = self.__dump write("<value><struct>\n") for k, v in value.items(): write("<member>\n") if type(k) is StringType: k = escape(k) elif unicode and type(k) is UnicodeType: k = escape(k).encode(self.encoding, 'xmlcharrefreplace') else: raise TypeError, "dictionary key must be string" write("<name>%s</name>\n" % k) dump(v, write) write("</member>\n") write("</struct></value>\n") del self.memo[i]
def _stringify(string): # convert to 7-bit ascii if possible try: return string.encode("ascii") except UnicodeError: return string
def encode(self, out): out.write("<value><boolean>%d</boolean></value>\n" % self.value)
def encode(self, out): out.write("<value><dateTime.iso8601>") out.write(self.value) out.write("</dateTime.iso8601></value>\n")
def encode(self, out): out.write("<value><base64>\n") base64.encode(StringIO.StringIO(self.data), out) out.write("</base64></value>\n")
def dump_instance(self, value, write): # check for special wrappers if value.__class__ in WRAPPERS: self.write = write value.encode(self) del self.write else: # store instance attributes as a struct (really?) self.dump_struct(value.__dict__, write)
def convert(filename): with open(filename, 'rb') as r: with open(os.path.splitext(filename)[0]) as w: base64.encode(r, w)
def login(self): credentials = base64.encode("{}:{}".format(self.username, self.password)) url = "{}:{}/cgi-bin/index.asp?" + credentials data = { "Loginuser": "supervisor", "Prestige_Login": "Login" } response = http_request(method="POST", url=url, data=data, session=self.session) if response is not None and response.status_code == 200: return True return False
def __init__(self, uri, transport=None, encoding=None, verbose=0, allow_none=0, use_datetime=0, context=None): # establish a "logical" server connection if isinstance(uri, unicode): uri = uri.encode('ISO-8859-1') # get the url import urllib type, uri = urllib.splittype(uri) if type not in ("http", "https"): raise IOError, "unsupported XML-RPC protocol" self.__host, self.__handler = urllib.splithost(uri) if not self.__handler: self.__handler = "/RPC2" if transport is None: if type == "https": transport = SafeTransport(use_datetime=use_datetime, context=context) else: transport = Transport(use_datetime=use_datetime) self.__transport = transport self.__encoding = encoding self.__verbose = verbose self.__allow_none = allow_none