专栏名称: Python开发者
人生苦短,我用 Python。伯乐在线旗下账号「Python开发者」分享 Python 相关的技术文章、工具资源、精选课程、热点资讯等。
目录
相关文章推荐
51好读  ›  专栏  ›  Python开发者

Python 互联网数据处理模块介绍

Python开发者  · 公众号  · Python  · 2017-04-08 19:30

正文

请到「今天看啥」查看全文


. b64encode ( 'a' )

>>> encoded

'YQ=='


Base64 解码参见快速入门部分介绍。


URL-Safe


•base64.urlsafe_b64encode(s):

•base64.urlsafe_b64decode(s):


Base64 默认会使用+和/, 但是这 2 个字符在 url 中也有特殊含义。使用 urlsafe 可以解决这个问题。 +替换为-, /替换为_。


import base64

encodes_with_pluses = chr ( 251 ) + chr ( 239 )

encodes_with_slashes = chr ( 255 ) * 2

for original in [ encodes_with_pluses , encodes_with _ slashes ] :

print 'Original

:' , repr ( original )

print 'Standard encoding:' , base64 . standard_b64encode ( original )

print 'URL-safe encoding:' , base64 . urlsafe_b64encode ( original )

print


➢执行结果


$ python base64_urlsafe . py

Original

: '\xfb\xef'

Standard encoding : ++ 8 =

URL - safe encoding : -- 8 =

Original

: '\xff\xff'

Standard encoding : // 8 =

URL - safe encoding : __8 =


其他编码

Base32 包含 26 个大写字母和 2-7 的数字。

• base64.b32encode(s):使用 Base32 编码字符串。s 是要编码的字符串。
• base64.b32decode(s[, casefold[, map01]]):解码 Base32 编码的字符串。s 为要解码的字符串 。

casefold 表示是否允许小写字母。 map01 表示允许 0 表示 0,1 表示 L 。

import base64

original_string = 'This is the data, in the clear.'

print 'Original:' , original_string

encoded_string = base64 . b32encode ( original_string )

print 'Encoded :' , encoded_string

decoded_string = base64 . b32decode ( encoded_string )

print 'Decoded :' , decoded_string < code class = "hljs stylus" > g


➢执行结果


$ python base64_base32 . py

Original : This is the data , in the clear .

Encoded :







请到「今天看啥」查看全文