Python base64模块详解
来源:互联网 发布:手机办公软件excel 编辑:程序博客网 时间:2024/05/20 01:10
Python base64模块是用来作base64编码解码的。
最简单的加解密实例:
Python base64模块真正用的上的方法只有8个,分别是encode, decode, encodestring, decodestring, b64encode,b64decode, urlsafe_b64decode,urlsafe_b64encode。他们8个可以两两分为4组,encode,decode一组,专门用来编码和解码文件的,也可以对StringIO里的数据做编解码;encodestring,decodestring一组,专门用来编码和解码字符串; b64encode和b64decode一组,用来编码和解码字符串,并且有一个替换符号字符的功能。这个功能是这样的:因为base64编码后的字符除 了英文字母和数字外还有三个字符 + / =, 其中=只是为了补全编码后的字符数为4的整数,而+和/在一些情况下需要被替换的,b64encode和b64decode正是提供了这样的功能。至于什么情况下+和/需要被替换,最常见的就是对url进行base64编码的时候。urlsafe_b64encode和urlsafe_b64decode 一组,这个就是用来专门对url进行base64编解码的,实际上也是调用的前一组函数。
Python base64模块加/解密例子:
上面的encode函数和decode函数的参数也可以是文件对象:
最简单的加解密实例:
import base64 str1 = 'djhui'str2 = base64.b64encode(str1) str3 = base64.b64decode(str2)
Python base64模块真正用的上的方法只有8个,分别是encode, decode, encodestring, decodestring, b64encode,b64decode, urlsafe_b64decode,urlsafe_b64encode。他们8个可以两两分为4组,encode,decode一组,专门用来编码和解码文件的,也可以对StringIO里的数据做编解码;encodestring,decodestring一组,专门用来编码和解码字符串; b64encode和b64decode一组,用来编码和解码字符串,并且有一个替换符号字符的功能。这个功能是这样的:因为base64编码后的字符除 了英文字母和数字外还有三个字符 + / =, 其中=只是为了补全编码后的字符数为4的整数,而+和/在一些情况下需要被替换的,b64encode和b64decode正是提供了这样的功能。至于什么情况下+和/需要被替换,最常见的就是对url进行base64编码的时候。urlsafe_b64encode和urlsafe_b64decode 一组,这个就是用来专门对url进行base64编解码的,实际上也是调用的前一组函数。
Python base64模块加/解密例子:
#-*- encoding:gb2312 -*- import base64 import StringIO a = "this is a test"b = base64.encodestring(a) # 对字符串编码 print b print base64.decodestring(b) # 对字符串解码 c = StringIO.StringIO() c.write(a) d = StringIO.StringIO() e = StringIO.StringIO() c.seek(0) base64.encode(c, d) # 对StringIO内的数据进行编码 print d.getvalue() d.seek(0) base64.decode(d, e) # 对StringIO内的数据进行解码 print e.getvalue() a = "this is a +test"b = base64.urlsafe_b64encode(a) # 进行url的字符串编码 print b print base64.urlsafe_b64decode(b)
上面的encode函数和decode函数的参数也可以是文件对象:
f1 = open('aaa.txt', 'r') f2 = open('bbb.txt', 'w') base64.encode(f1, f2) f1.close() f2.close()
0 0
- Python base64模块详解
- python模块之base64
- python的模块base64
- python模块之base64
- python模块之base64
- python模块之base64
- Python base64模块
- Python(base64)编码模块
- Python模块介绍之base64模块、Queue模块
- base64编码原理以及python中的base64模块
- [Python模块学习]使用base64模块进行二进制数据编码
- base64 模块
- Python模块详解
- Python模块详解
- python time模块详解
- python time模块详解
- python time模块详解
- python time模块详解
- 美国有名的创业大王雷·克利斯
- ODBC相关
- 要过中秋节了
- [ZT]教你几招看透一个人
- [ZT]当避孕套价格超过内裤的时候
- Python base64模块详解
- [ZT]安全经过广州火车站攻略
- 解决添加打印机时出现错误两则:
- 配了一台电脑,如下配置
- 上完今天下午的班,就回家了
- Oracle一存储过程总结
- 回来了,上了一周的班
- 很累
- pureMVC 学习笔记