import jsonpath,json
data = {
“store”: {
“book”: [
{
“category”: “reference”,
“author”: “Nigel Rees”,
“title”: “Sayings of the Century”,
“price”: 8.95
},
{
“category”: “fiction”,
“author”: “Evelyn Waugh”,
“title”: “Sword of Honour”,
“price”: 12.99
},
{
“category”: “fiction”,
“author”: “Herman Melville”,
“title”: “Moby Dick”,
“isbn”: “0-553-21311-3”,
“price”: 8.99
},
{
“category”: “fiction”,
“author”: “J. R. R. Tolkien”,
“title”: “The Lord of the Rings”,
“isbn”: “0-395-19395-8”,
“price”: 22.99
}
],
“bicycle”: {
“color”: “red”,
“price”: 19.95
}
},
“expensive”: 10
}
# 基于JsonPath获取元素:通过jsonpath函数来获取(json数据,定位表达式)
“”"
jsonpath表达式的基本格式规范:
$ 表示根节点,也是所有jsonpath表达式的开始
. 表示获取子节点
… 表示获取所有符合条件的内容
* 代表所有的元素节点
[] 表示迭代器的标示(可以用于处理下标等情况)
[,] 表示多个结果的选择
?() 表示过滤操作
@ 表示当前节点
“”"
bike=jsonpath.jsonpath(data,‘$.store.bicycle.color’)
print(bike)
print(type(bike))
book=jsonpath.jsonpath(data,‘$.store.book’)
print(book)
price=jsonpath.jsonpath(data,‘$.store…price’)
print(price)
book_price=jsonpath.jsonpath(data,“$.store.book[0,1,2].price”)
print(book_price)
book_1=jsonpath.jsonpath(data,“$…book[?(@.price>10)]”)
print(book_1)
#md5加密
import hashlib
arg =“wujun”
se=hashlib.md5(arg.encode(‘utf-8’))
print(se)
print(se.hexdigest())
def hashmd5(string):
return hashlib.md5(arg.encode(‘utf-8’)).hexdigest()
se2=hashmd5(arg)
print(se2)
#aes加密
import base64
from Crypto.Cipher import AES
class EncryptDate:
def init(self,key):
self.key=key.encode(“utf-8”)
self.length=AES.block_size
self.aes=AES.new(self.key,AES.MODE_ECB)
self.unpad=lambda date:date[0:-ord(date[-1])]
def pad(self,text):
count=len(text.encode(“utf-8”))
add=self.length-(count % self.length)
entext=text+(chr(add)*add)
return entext
def encrypt(self,encrData):
res=self.aes.encrypt(self.pad(encrData).encode(“utf-8”))
msg=str(base64.b64encode(res),encoding=“utf8”)
return msg
def decrypt(self,decrData):
res=base64.decodebytes(decrData.encode(“utf8”))
msg=self.aes.decrypt(res).decode(“utf8”)
return self.unpad(msg)
if name ==‘main’:
print(“加密=”)
key=‘1234567812345678’
data=‘tony’
eg=EncryptDate(key)
res=eg.encrypt(str(data))
print(res,end=‘’)
print(“\n解密=====”)
data=“XbXHJrNLwoTVcyfqM9eTgQ==”
res=eg.decrypt(str(data))
print(res,end=‘’)
#!/usr/bin/env python
-- coding: utf-8 --
import base64
import rsa
秘钥的位数, 可以自定义指定, 例如: 128、256、512、1024、2048等
(pubkey, privkey) = rsa.newkeys(512)
生成公钥
pub = pubkey.save_pkcs1()
with open(‘public.pem’, ‘wb’) as f:
f.write(pub)
# 生成私钥
pri = privkey.save_pkcs1()
with open(‘private.pem’, ‘wb’) as f:
f.write(pri)
pub_str = ‘’'-----BEGIN RSA PUBLIC KEY-----
MCgCIQCR8RiQaYZ8AwSt/EVMVs0Y88qqWGW0pYFdCgfi8G0z1QIDAQAB
-----END RSA PUBLIC KEY-----
‘’’
priv_str = ‘’'-----BEGIN RSA PRIVATE KEY-----
MIGqAgEAAiEAkfEYkGmGfAMErfxFTFbNGPPKqlhltKWBXQoH4vBtM9UCAwEAAQIg
WP1kdxqBkLjeb6D2OJhsqUtU9+E+KkG3+4/DHH6mJYECEgDi1MtnvlIeYE2YX2Rr
1/HiJQIQAKS1cuWHyxZ/PTFR50dj8QIRWnsDgCh83T0Q9rzmc9Y52DkCDzw0QroZ
Z6difOiRKnuS0QIRX2j3UMIeTiom54alccbRQ4Y=
-----END RSA PRIVATE KEY-----
‘’’
pub_str = ‘’‘-----BEGIN RSA PUBLIC KEY-----
MEgCQQCte/qARce725ilnKCQ4ULrBxhECGygdUq022KUTpd/oPYHQkBCFhup7TtW
QAstYinLQQantDayhDcCTiwFYnGxAgMBAAE=
-----END RSA PUBLIC KEY-----’‘’
priv_str = ‘’‘-----BEGIN RSA PRIVATE KEY-----
MIIBPQIBAAJBAK17+oBFx7vbmKWcoJDhQusHGEQIbKB1SrTbYpROl3+g9gdCQEIW
G6ntO1ZACy1iKctBBqe0NrKENwJOLAVicbECAwEAAQJBAJ0VLjwpcvIBo5UJXkXx
AdOnx+wKKLmdmrhqETeydTqaAAYt4wdTsw6Ik03GK+JoP7/NHKCsTy+T/pv288QQ
29UCIwDJd4+LQgJcg45w+SR7jrI8xZJLS+1aC68yTsmTm2Ox6psHAh8A3HFiT2A9
JxLLEc1qrnOHcWFkXQnR8jskyr5xRoeHAiJTyZ2xVOqWf0AmS/eYS2hlTeXh2toD
sQhT6+B5uw5jWCbRAh8AjUeVrC2Pw8UFpKk0IA4m1gGHIyGyDj7de5rGWl9tAiIR
a6ls8EL2C27mnhtG2otMHUbZYnGXA5TYyw0upZ0Ogo5u
-----END RSA PRIVATE KEY-----’‘’
class Rsa():
def init(self):
self.pub_key = rsa.PublicKey.load_pkcs1(pub_str)
self.priv_key = rsa.PrivateKey.load_pkcs1(priv_str)
def encrypt(self, text):
# rsa加密 最后把加密字符串转为base64
text = text.encode("utf-8")
cryto_info = rsa.encrypt(text, self.pub_key)
cipher_base64 = base64.b64encode(cryto_info)
cipher_base64 = cipher_base64.decode()
return cipher_base64
def decrypt(self, text):
# rsa解密 返回解密结果
cryto_info = base64.b64decode(text)
talk_real = rsa.decrypt(cryto_info, self.priv_key)
res = talk_real.decode("utf-8")
return res
if name == “main”:
rsaer = Rsa()
info = rsaer.encrypt(‘展昭’)
print(‘加密:’, info)
print(‘解密:’, rsaer.decrypt(info))
# 加密: HcbXuD4kkHKzwC4h2G7S2EPNG5O1RnOrisstGF1lgi4=
# 解密: 展昭