【问题标题】:How decode asn1 hex value using asn1tools如何使用 asn1tools 解码 asn1 十六进制值
【发布时间】:2021-03-26 08:26:02
【问题描述】:

我正在尝试使用 Python 解码以下 ASN1 消息

b'\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x02\x89G\x11\x00\x1a\x01\x00\x10\x00\x80\x00\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x02\x98c\xc7h\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

这是我的代码:

import asn1tools
import socket
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = ('239.118.122.97', 8947)
#server_address = ('localhost', 8947)
#server_address = ('192.168.1.63', 8948)
#server_address = ('0.0.0.0', 8947)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)

while True:
    print('\nwaiting to receive message')
    data, address = sock.recvfrom(8947)

    print('\nreceived {} bytes from {}'.format(
        len(data), address))
    print(data)
    foo = asn1tools.compile_files(data,'uper')

我收到以下错误:

Traceback (most recent call last):
  File "udp.py", line 21, in <module>
    foo = asn1tools.compile_files(data,'uper')
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/compiler.py", line 376, in compile_files
    return compile_dict(parse_files(filenames, encoding),
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/parser.py", line 1877, in parse_files
    with open(filename, 'r', encoding=encoding, errors='replace') as fin:
OSError: [Errno 9] Bad file descriptor

我希望收到 JSON 格式的消息。

【问题讨论】:

  • 正在寻找这个...找到什么了吗?

标签: python json udp converters asn1


【解决方案1】:

快速查看文档表明 compile_files 用于编译 ASN 规范(看起来它需要一个文件名)。您似乎正试图在编码数据上使用它。

【讨论】:

  • 我也尝试过f = asn1tools convert -i uper -o jer data,但无法成功运行
  • 您需要弄清楚如何将 ASN 规范提供给您的工具。不参考规范就无法解码 PER 数据。
【解决方案2】:

如果 var b'\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x02\x89G\x11\x00\x1a\x01\x00\x10\x00\x80\ x00\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x02\x98c\xc7h\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00'

decoded_var=var.hex()

【讨论】:

    猜你喜欢
    • 2021-06-10
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2020-11-11
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    相关资源
    最近更新 更多