'''
The following Python Code running environment is windows10,
Python version is 3.5.3
Libraries involved: 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...")
# Encrypt json_string
def test_request_json_encryption(self):
# Convert json_ of dict type to json_ of str type of bytes byte stream
json_encryption = base64.b64encode(json.dumps(self.json_).encode('utf-8'))
print(json_encryption)
return json_encryption
# decrypt json_string
def test_request_json_Decrypt(self):
# --------------------------------------------------------------------------------------------------------
json_encryption = self.test_request_json_encryption()
# Decrypt and decode the str type json of the bytes byte stream (default is utf-8)
# b'{"password": "001", "username": "001", "uuid": "F9E73F13915A6283F4D9916C36E6D867"}'
json_decrypt = base64.b64decode(json_encryption.decode('utf-8'))
print(json_decrypt)
# --------------------------------------------------------------------------------------------------------
# Convert the 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 getting str type data, it needs to be converted to dict type to get the values of a key
json_encryption_dict = json.loads(json_encryption_str)
print(type(json_encryption_dict))
# -------------------------------------------------------------------------------------------------------
# For example, to take out the value of "uuid"
json_encryption_dict_uuid = json_encryption_dict["uuid"]
print(json_encryption_dict_uuid)
if __name__ == '__main__':
unittest.main()