ParseError:第1行第1列的ASN.1语法无效:'>!<“':应为modulereferen

2024-04-24 04:17:28 发布

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

我是asn1新手,我的议程是我想把python字典转换成.asn格式。 当我运行下面的代码时,我得到了以下错误

ParseError:第1行第1列的ASN.1语法无效:'>;!<;“':应为modulereference。你知道吗

from __future__ import print_function
from binascii import hexlify
import asn1tools

specification=""""
Foo DEFINITIONS ::= BEGIN

    Question ::= SEQUENCE {
        id        INTEGER,
        question  IA5String
    }

    Answer ::= SEQUENCE {
        id        INTEGER,
        answer    BOOLEAN
    }

END
""""


Foo = asn1tools.compile_string(specification, 'uper')

Question = {'id': 2, 'question': u'Hi how r u?!'}
Answer ={'id': 2, 'answer': u'Hi i am good'}
encoded = Foo.encode('Question', Question)
encoded1 = Foo.encode('Answer', Answer)
decoded = Foo.decode('Question', Question)

print('Question:', Question)
print('Encoded:', hexlify(encoded).decode('ascii'))
print('Decoded:', decoded)

Tags: answerfromimportidfoointegerhispecification
2条回答

您的ASN.1架构看起来是正确的。您可以在asn1.io验证语法。由于报告的错误是第一个字符(第1行第1列),因此可能是在准备规范时插入的额外引号或其他字符。。你知道吗

Python字符串文本不是用四个引号括起来的,而是三个引号。你知道吗

从问题中突出显示的语法可以看出这是错误的。你知道吗

我的Python安装完全拒绝您的代码。当我将结束分隔符修改为三个引号(但保持开头分隔符不变)时,我得到了您报告的问题。(请下次逐字张贴您的代码。)

当我同时修复这两个问题时,会出现一个新错误:

asn1tools.codecs.EncodeError: answer: Expected data of type bool, but got Hi i am good.

这是因为您试图像布尔值一样使用英语字符串;它应该是:

Answer ={'id': 2, 'answer': True}

最后,解码失败,因为您将错误的参数传递给Foo.decode;它应该是:

decoded = Foo.decode('Question', encoded)

现在起作用了。你知道吗


from __future__ import print_function
from binascii import hexlify
import asn1tools

specification="""
Foo DEFINITIONS ::= BEGIN

    Question ::= SEQUENCE {
        id        INTEGER,
        question  IA5String
    }

    Answer ::= SEQUENCE {
        id        INTEGER,
        answer    BOOLEAN
    }

END
"""


Foo = asn1tools.compile_string(specification, 'uper')

Question = {'id': 2, 'question': u'Hi how r u?!'}
Answer ={'id': 2, 'answer': True}
encoded = Foo.encode('Question', Question)
encoded1 = Foo.encode('Answer', Answer)
decoded = Foo.decode('Question', encoded)

print('Question:', Question)
print('Encoded:', hexlify(encoded).decode('ascii'))
print('Decoded:', decoded)

相关问题 更多 >