Python3 base64编码&str2dict

Python3 base64编码细节

base64输入需要进行编码,base64输出是byte类型

import base64
index1 = "jack-2021.04.28"

def base64_encode(strings):
    str1 = base64.b64encode(strings.encode("utf-8"))
    encode_data = str(str1, 'utf-8')
    return encode_data

def base64_decode(strings):
    str1 = base64.b64decode(strings.encode('utf-8'))
    decode_data = str(str1,'utf-8')
    return decode_data

index2 = base64_encode(index1)
print(index2)

输出

amFjay0yMDIxLjA0LjI4

python3 字符串转dict

import json
qbody1 = {"query":{"bool":{"must":[],"filter":[{"match_all":{}},{"match_phrase":{"manager.name":{"query":"ubuntu"}}},{"match_phrase":{"agent.id":{"query":"004"}}},{"range":{"timestamp":{"from":"now-24h","to":"now"}}}],"should":[],"must_not":[]}},"aggs":{},"size":0}

jsonencode = json.dumps(qbody1) # dict to str
qbodyen = base64_encode(jsonencode)
qbodyde = base64_decode(qbodyen)
qbodydict = json.loads(qbodyde)
print(type(qbody1))
print(type(jsonencode))
print(qbodyen)
print(type(qbodyde))
print(qbodyde)
print(type(qbodydict))

输出结果

<class 'dict'>
<class 'str'>
eyJxdWVyeSI6IHsiYm9vbCI6IHsibXVzdCI6IFtdLCAiZmlsdGVyIjogW3sibWF0Y2hfYWxsIjoge319LCB7Im1hdGNoX3BocmFzZSI6IHsibWFuYWdlci5uYW1lIjogeyJxdWVyeSI6ICJ1YnVudHUifX19LCB7Im1hdGNoX3BocmFzZSI6IHsiYWdlbnQuaWQiOiB7InF1ZXJ5IjogIjAwNCJ9fX0sIHsicmFuZ2UiOiB7InRpbWVzdGFtcCI6IHsiZnJvbSI6ICJub3ctMjRoIiwgInRvIjogIm5vdyJ9fX1dLCAic2hvdWxkIjogW10sICJtdXN0X25vdCI6IFtdfX0sICJhZ2dzIjoge30sICJzaXplIjogMH0=
{"query": {"bool": {"must": [], "filter": [{"match_all": {}}, {"match_phrase": {"manager.name": {"query": "ubuntu"}}}, {"match_phrase": {"agent.id": {"query": "004"}}}, {"range": {"timestamp": {"from": "now-24h", "to": "now"}}}], "should": [], "must_not": []}}, "aggs": {}, "size": 0}
<class 'str'>
<class 'dict'>
posted @ 2021-08-26 21:08  Right2014  阅读(326)  评论(0)    收藏  举报