'''
以下 Python Code 运行环境为 windows10,
Python 版本为 3.5.3
涉及的库: base64,json,unittest
- '''
- # coding=utf-8
- # import requests
- # import hashlib
- import base64, json
- import unittest
- class 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...")
- # 加密 json_字符串
- def test_request_json_encryption(self):
- # 把 dict 类型的 json_转换成 bytes 字节流的 str 类型的 json_
- json_encryption = base64.b64encode(json.dumps(self.json_).encode('utf-8'))
- print(json_encryption)
- return json_encryption
- # 解密 json_字符串
- def test_request_json_Decrypt(self):
- # --------------------------------------------------------------------------------------------------------
- json_encryption = self.test_request_json_encryption()
- # 把 bytes 字节流的 str 类型的 json 进行解密 decode(默认为 utf-8)
- # b'{"password":"001","username":"001","uuid":"F9E73F13915A6283F4D9916C36E6D867"}'
- json_decrypt = base64.b64decode(json_encryption.decode('utf-8'))
- print(json_decrypt)
- # --------------------------------------------------------------------------------------------------------
- # 把解密后的 bytes 类型数据转换成 str 类型数据
- # {"uuid": "F9E73F13915A6283F4D9916C36E6D867", "password": "001", "username": "001"}
- json_encryption_str = json_decrypt.decode()
- print(json_encryption_str)
- # -------------------------------------------------------------------------------------------------------
- # 得到 str 类型数据后, 要转换成 dict 类型, 才能取出某个 key 的 values
- json_encryption_dict = json.loads(json_encryption_str)
- print(type(json_encryption_dict))
- # -------------------------------------------------------------------------------------------------------
- # 比如要取出 "uuid" 的 value
- json_encryption_dict_uuid = json_encryption_dict["uuid"]
- print(json_encryption_dict_uuid)
- if __name__ == '__main__':
- unittest.main()
来源: http://www.bubuko.com/infodetail-2582304.html