Python xmlrpclib base64 encoding

Source: Internet
Author: User

In xmlrpclib, Binary data needs to be transmitted. Therefore, Binary Objects is used for packaging and the following two functions are written:

Def encodebindata (data ):
Out = StringIO. StringIO ()
Bin = xmlrpclib. Binary ()
Bin. data = data
Bin. encode (out)
Data2 = out. getvalue ()
Return data2

Def decodebindata (data ):
Bin = xmlrpclib. Binary ()
Data1 = bin. decode (data)
Return bin. data

When the decodebindata function is called for decoding, there are always errors and prompts such as "binascii. Error: incorrect padding. Let's take a look at the Binary class definition in xmlrpclib. py:

Def decode (self, data ):
Self. data = base64.decodestring (data)

Def encode (self, out ):
Out. write ("<value> <base64> n ")
Base64.encode (StringIO. StringIO (self. data), out)
Out. write ("</base64> </value> n ")

Binary encoding and decoding actually uses the decodestring and encode of base64, which does not seem to match very well. In base64, encode should be paired with decode, and decodestring and encodestring should be paired. Therefore, we tried to directly use base64 for packaging without using the Binary Objects of xmlrpclib. The two functions are redefined as follows:

Def encodebindata (data ):
Out = StringIO. StringIO ()
Bin = StringIO. StringIO (data)
Base64.encode (bin, out)
Data2 = out. getvalue ()
Return data2

Def decodebindata (data ):
Out = StringIO. StringIO ()
Bin = StringIO. StringIO (data)
Base64.decode (bin, out)
Data2 = out. getvalue ()
Return data2

After testing, there are no problems

The Binary Objects of xmlrpclib is packaged in xml format and can be directly transmitted. You cannot unpack the package immediately after packaging.

From: http://blog.robotercoding.com /? Cat = 5 & paged = 2


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.