[英]Python output to a .json format
這是“實時”從捕獲的數據包中提取 TLS 客戶端 Hello 信息的程序的一部分。
def parse_client_hello(handshake):
if isinstance(handshake.data, dpkt.ssl.TLSClientHello):
client = dpkt.ssl.TLSClientHello(str(handshake.data))
print(' (***) The version of the TLS supported by the client: {0}'
.format(tls_dictionary('tls_version',client.version)))
session_id, pointer = parse(client.data, 1)
print(' (***) The session ID of the client: {0} '
.format(hexlify(session_id)))
ciphersuites, pointer1 = parse(client.data[pointer:], 2)
ciphersuites, pretty_cipher_suites = parse_extension(ciphersuites, 'cipher_suites')
print(' (***) The cipher suites proposed by the client: {0} '
.format(pretty_cipher_suites))
print(' (***) The random of the client: {0} '.format(client.random))
pointer += pointer1
compression_methods, pointer1 = parse(client.data[pointer:], 1)
compression_methods, pretty_compressions = parse_extension(compression_methods,
'compression_methods')
print(' (***) The compression methods: {0} '.format(pretty_compressions))
sys.stdout.flush()
終端顯示的這部分的output為:
(***) The version of the TLS supported by the client: TLS 1.2
(***) The session ID of the client: f72434d3e6d82d0798a78192516ba69623603a6d358a6f17642fc34dc67bab72
(***) The cipher suites proposed by the client: ['TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256', 'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256', 'TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256', 'TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256', 'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384', 'TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384', 'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA', 'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA', 'TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA', 'TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA', 'TLS_RSA_WITH_AES_128_GCM_SHA256', 'TLS_RSA_WITH_AES_256_GCM_SHA384', 'TLS_RSA_WITH_AES_128_CBC_SHA', 'TLS_RSA_WITH_AES_256_CBC_SHA', 'TLS_RSA_WITH_3DES_EDE_CBC_SHA']
(***) The random of the client: �.�45���M�܌ s=�����GIA��k~��
(***) The compression methods: ['null']
我的目標是美化 output 數據並將其轉換為 a.json 格式,並且 output 應打印在文件中。
我想要得到的是這樣的:
Version: TLS 1.2
Session ID: f72434d3e6d82d0798a78192516ba69623603a6d358a6f17642fc34dc67bab72
Cipher Suites: ['TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256', 'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256','TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256', 'TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256','TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384', 'TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384','TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA', 'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA','TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA', 'TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA','TLS_RSA_WITH_AES_128_GCM_SHA256', 'TLS_RSA_WITH_AES_256_GCM_SHA384', 'TLS_RSA_WITH_AES_128_CBC_SHA', 'TLS_RSA_WITH_AES_256_CBC_SHA', 'TLS_RSA_WITH_3DES_EDE_CBC_SHA']
Random: �.�45���M�܌ s=�����GIA��k~��
Compression Method: null
你知道我應該從哪里開始或有什么建議嗎?
將您想要的所有數據放入字典中:
obj = {}
obj["Version"] = ...
obj["Session ID"] = ...
...
或內聯
obj = {"Version": ..., "Session ID": ..., ...}
並使用 json 庫將其轉儲到文件中:
import json
with open(filename, "w") as f:
json.dump(obj, f)
當然。
創建一個這樣的字典:
response = {
'Version': tls_dictionary('tls_version',client.version)
'Session ID': hexlify(session_id)
'Cipher Suites': pretty_cipher_suites
'Random': client.random
'Compression Method': pretty_compressions
}
然后json_response = json.dumps(response)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.