python模块介绍- base64 使用ASCII编码二进制数据

长平狐 发布于 2013/12/25 17:23
阅读 134
收藏 0

python模块介绍-base64 使用ASCII编码二进制数据

2013-05-13 磁针石

#承接软件自动化实施与培训等gtalk:ouyangchongwu#gmail.comqq 37391319 博客:http://blog.csdn.net/oychw

#版权所有,转载刊登请来函联系

#深圳测试自动化python项目接单群113938272深圳会计软件测试兼职 6089740

#深圳地摊群 66250781武冈洞口城步新宁乡情群49494279

#自动化测试和python群组:http://groups.google.com/group/automation_testing_python

#参考资料:《The PythonStandard Libraryby Example 2011》

      

       转换二进制数据为适合明文协议传输的ASCII序列。转换8bits为每个字节包含6,5或4bits的有效数据,比如SMTP,URL的一部分或者HTTPPOST的一部分。参考:RFC 3548。编码算法不同于uuencode。

       Base64编码:

#end_pymotw_header

 

import base64

import textwrap

 

# Load this source file and strip the header.

with open(__file__, 'rt') as input:

    raw =input.read()

   initial_data = raw.split('#end_pymotw_header')[1]

 

encoded_data = base64.b64encode(initial_data)

 

num_initial = len(initial_data)

 

# There will never be more than 2 padding bytes.

padding = 3 - (num_initial % 3)

 

print '%d bytes before encoding' % num_initial

print 'Expect %d padding bytes' % padding

print '%d bytes after encoding' % len(encoded_data)

print

print encoded_data

执行结果:

$ python base64_b64encode.py

168 bytes before encoding

Expect 3 padding bytes

224 bytes after encoding

CgppbXBvcnQgYmFzZTY0CmltcG9ydCB0ZXh0d3JhcAoKIyBMb2FkIHRoaXMgc291c

mNlIGZpbGUgYW5kIHN0cmlwIHRoZSBoZWFkZXIuCndpdGggb3BlbihfX2ZpbGVfXy

wgJ3J0JykgYXMgaW5wdXQ6CiAgICByYXcgPSBpbnB1dC5yZWFkKCkKICAgIGluaXR

pYWxfZGF0YSA9IHJhdy5zcGxpdCgn

 

b64decode()通过把4bytes转为3bytes实现解码。

#end_pymotw_header

 

import base64

 

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

print 'Original:', original_string

 

encoded_string = base64.b64encode(original_string)

print 'Encoded :', encoded_string

 

decoded_string = base64.b64decode(encoded_string)

print 'Decoded :', decoded_string

 

执行结果:

$ python base64_b64decode.py

Original: This is the data, in the clear.

Encoded : VGhpcyBpcyB0aGUgZGF0YSwgaW4gdGhlIGNsZWFyLg==

Decoded : This is the data, in the clear.

 

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

#end_pymotw_header

 

importbase64

 

encodes_with_pluses= chr(251) + chr(239)

encodes_with_slashes= chr(255) * 2

 

fororiginal 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’

Standardencoding: ++8=

URL-safeencoding: --8=

Original: ’\xff\xff’

Standardencoding: //8=

URL-safeencoding: __8=

 

其他编码:Base32包含26个大写字母和2-7的数字。

__version__= "$Id$"

#end_pymotw_header

 

importbase64

 

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

执行结果:

$python base64_base32.py

Original:This is the data, in the clear.

Encoded: KRUGS4ZANFZSA5DIMUQGIYLUMEWCA2LOEB2GQZJAMNWGKYLSFY======

Decoded: This is the data, in the clear.

 

其他编码:Base16包含16个16进制大写数字。

__version__= "$Id$"

#end_pymotw_header

 

importbase64

 

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

print'Original:', original_string

 

encoded_string= base64.b16encode(original_string)

print'Encoded :', encoded_string

 

decoded_string= base64.b16decode(encoded_string)

print'Decoded :', decoded_string

执行结果:

$python base64_base16.py

Original:This is the data, in the clear.

Encoded: 546869732069732074686520646174612C20696E2074686520636C6561

722E

Decoded: This is the data, in the clear.

 

 

 

 

             

 

 


原文链接:http://blog.csdn.net/oychw/article/details/8920653
加载中
返回顶部
顶部