Package: pysnmp-pyasn1
ASN.1 library for PythonThis is a free and open source implementation of ASN.1 types and codecs
as a Python package. It has been first written to support particular
protocol (SNMP) but then generalized to be suitable for a wide range
of protocols based onASN.1 specification.This project has been forked for ongoing maintenance and modernization.FeaturesGeneric implementation of ASN.1 types (X.208)Standards compliant BER/CER/DER codecsCan operate on streams of serialized dataDumps/loads ASN.1 structures from Python types100% Python, works with Python 2.7 and 3.5+MT-safeContributed ASN.1 compilerAsn1ateWhy using pyasn1ASN.1 solves the data serialisation problem. This solution was
designed long ago by the wise Ancients. Back then, they did not
have the luxury of wasting bits. That is why ASN.1 is designed
to serialise data structures of unbounded complexity into
something compact and efficient when it comes to processing
the data.That probably explains why many network protocols and file formats
still rely on the 30+ years old technology. Including a number of
high-profile Internet protocols and file formats.Quite a number of books cover the topic of ASN.1.Communication between heterogeneous systemsby Olivier Dubuisson is one of those high quality books freely
available on the Internet.The pyasn1 package is designed to help Python programmers tackling
network protocols and file formats at the comfort of their Python
prompt. The tool struggles to capture all aspects of a rather
complicated ASN.1 system and to represent it on the Python terms.How to use pyasn1With pyasn1 you can build Python objects from ASN.1 data structures.
For example, the following ASN.1 data structure:Record::=SEQUENCE{idINTEGER,room[0]INTEGEROPTIONAL,house[1]INTEGERDEFAULT0}Could be expressed in pyasn1 like this:classRecord(Sequence):componentType=NamedTypes(NamedType('id',Integer()),OptionalNamedType('room',Integer().subtype(implicitTag=Tag(tagClassContext,tagFormatSimple,0))),DefaultedNamedType('house',Integer(0).subtype(implicitTag=Tag(tagClassContext,tagFormatSimple,1))))It is in the spirit of ASN.1 to take abstract data description
and turn it into a programming language specific form.
Once you have your ASN.1 data structure expressed in Python, you
can use it along the lines of similar Python type (e.g. ASN.1SETis similar to Pythondict,SET OFtolist):>>>record=Record()>>>record['id']=123>>>record['room']=321>>>str(record)Record:id=123room=321>>>Part of the power of ASN.1 comes from its serialisation features. You
can serialise your data structure and send it over the network.>>>frompyasn1.codec.der.encoderimportencode>>>substrate=encode(record)>>>hexdump(substrate)00000:300702017B80020141Conversely, you can turn serialised ASN.1 content, as received from
network or read from a file, into a Python object which you can
introspect, modify, encode and send back.>>>frompyasn1.codec.der.decoderimportdecode>>>received_record,rest_of_substrate=decode(substrate,asn1Spec=Record())>>>>>>forfieldinreceived_record:>>>print('{}is{}'.format(field,received_record[field]))idis123roomis321houseis0>>>>>>record==received_recordTrue>>>received_record.update(room=123)>>>substrate=encode(received_record)>>>hexdump(substrate)00000:300602017B80017BThe pyasn1 classes struggle to emulate their Python prototypes (e.g. int,
list, dict etc.). But ASN.1 types exhibit more complicated behaviour.
To make life easier for a Pythonista, they can turn their pyasn1
classes into Python built-ins:>>>frompyasn1.codec.native.encoderimportencode>>>encode(record){'id':123,'room':321,'house':0}Or vice-versa -- you can initialize an ASN.1 structure from a tree of
Python objects:>>>frompyasn1.codec.native.decoderimportdecode>>>record=decode({'id':123,'room':321,'house':0},asn1Spec=Record())>>>str(record)Record:id=123room=321>>>With ASN.1 design, serialisation codecs are decoupled from data objects,
so you could turn every single ASN.1 object into many different
serialised forms. As of this moment, pyasn1 supports BER, DER, CER and
Python built-ins codecs. The extremely compact PER encoding is expected
to be introduced in the upcoming pyasn1 release.More information on pyasn1 APIs can be found in thedocumentation,
compiled ASN.1 modules for different protocols and file formats
could be found in the pyasn1-modulesrepo.How to get pyasn1This project is forked from pyasn1. The pyasn1 package is distributed under terms
and conditions of 2-clause BSDlicense.
Source code is freely available as a GitHubrepo.You couldpip install pysnmp-pyasn1or download it fromPyPI.If something does not work as expected,open an issueat GitHub or
post your questionon Stack Overflowor try browsing pyasn1 .Copyright (c) 2005-2020,Ilya Etingof.
All rights reserved.
Copyright (c) 2021- [Splunk Inc.]