【问题标题】:Python output to a .json formatPython 输出为 .json 格式
【发布时间】:2020-11-04 02:20:38
【问题描述】:

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

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

【问题讨论】:

  • 请重复[如何提问](stackoverflow.com/help/how-to-ask),来自intro tour。要求我们教你如何设计自定义程序对于 Stack Overflow 来说是题外话。
  • 你意识到预期的格式不是json格式吗?

标签: python json type-conversion


【解决方案1】:

将你想要的所有数据放入字典中:

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

或内联

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

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

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

【讨论】:

    【解决方案2】:

    当然。

    像这样创建一个字典:

    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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多