On Python implementation of interface Base64 and decryption method

Source: Internet
Author: User

‘‘‘

The following Python code runtime environment is WINDOWS10,

Python version is 3.5.3

Libraries involved: Base64,json,unittest

‘‘‘

# Coding=utf-8# import Requests# import Hashlibimport base64, JSONImport UnitTestclass Demorequests (unittest. TestCase):def setUp (self):#self. _url = ' http://www.baidu.com 'Self.json_ = {" username": "001"," password": "001","UUID": "f9e73f13915a6283f4d9916c36e6d867"}def tearDown (self):print ("Request_test_end ...")# Encrypt Json_ stringdef test_request_json_encryption (self):# Convert the dict type of Json_ to the bytes byte stream of the str type Json_json_encryption = Base64.b64encode (Json.dumps (Self.json_). Encode (' Utf-8 '))print (json_encryption)return json_encryption# Decrypt Json_ stringdef test_request_json_decrypt (self):# --------------------------------------------------------------------------------------------------------json_encryption = self.test_request_json_encryption ()# Decrypt the json of str type bytes byte stream decode (default = Utf-8)# b ' {"Password": "001", "username": "001", "UUID": "f9e73f13915a6283f4d9916c36e6d867"} 'Json_decrypt = Base64.b64decode (Json_encryption.decode (' Utf-8 '))print (Json_decrypt)# --------------------------------------------------------------------------------------------------------# Convert decrypted bytes type data to str type data# {"UUID": "f9e73f13915a6283f4d9916c36e6d867", "Password": "001", "username": "001"}json_encryption_str = Json_decrypt.decode ()print (JSON_ENCRYPTION_STR)# -------------------------------------------------------------------------------------------------------# After obtaining the STR type data, it is necessary to convert to the Dict type to remove the values of a key.json_encryption_dict = json.loads (json_encryption_str)print (Type (json_encryption_dict)) # -------------------------------------------------------------------------------------------------------# For example, to remove the "UUID" valuejson_encryption_dict_uuid = json_encryption_dict["uuid"]print (JSON_ENCRYPTION_DICT_UUID)if __name__ = = ' __main__ ':Unittest.main ()

On Python implementation of interface Base64 and decryption method

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.