• 关于Python实现Interface base64加解密方法


    '''

    以下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()
     
  • 相关阅读:
    网络控制芯片AX88796B系列使用简介
    word的样式设置
    Xilinx FPGA使用——ROM初始化文件
    我的2017
    STM32基础分析——USART的DMA模式
    STM32基础分析——PWM配置
    当我在看简历的时候,我在看什么……
    FPGA基础学习(4) -- 时序约束(理论篇)
    如何下载完整版专利
    查看相关专利的好处
  • 原文地址:https://www.cnblogs.com/mrchenyushen/p/8975441.html
Copyright © 2020-2023  润新知