Python输出为.json格式

2024-06-02 05:22:09 发布

您现在位置:Python中文网/ 问答频道 /正文

这是“实时”从捕获的数据包中提取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()

终端上显示的该部件的输出为:

    (***) 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'] 

我的目标是对输出数据进行预处理,并将其转换为.json格式,输出应打印在文件中

我想要得到的是这样的东西:

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

你知道我应该从哪里开始,或者有什么建议吗


Tags: oftheclientparsewithtlsecdsarsa
2条回答

将所需的所有数据放入字典:

obj = {}
obj["Version"] = ...
obj["Session ID"] = ...
...

或内联

obj = {"Version": ..., "Session ID": ..., ...}

并使用json库将其转储到文件中:

import json
with open(filename, "w") as f:
    json.dump(obj, f)

当然

创建一个这样的dict:

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)

相关问题 更多 >