Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

ASN.1 library for Python

PyPI Python Versions CI GitHub license

This 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 on ASN.1 specification.

This project has been forked for ongoing maintenance and modernization.

Features

  • Generic implementation of ASN.1 types (X.208)
  • Standards compliant BER/CER/DER codecs
  • Can operate on streams of serialized data
  • Dumps/loads ASN.1 structures from Python types
  • 100% Python, works with Python 3.10+
  • MT-safe
  • Contributed ASN.1 compiler Asn1ate

Why using pyasn1

ASN.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 systems by 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 pyasn1

With pyasn1 you can build Python objects from ASN.1 data structures. For example, the following ASN.1 data structure:

Record ::= SEQUENCE {
  id        INTEGER,
  room  [0] INTEGER OPTIONAL,
  house [1] INTEGER DEFAULT 0
}

Could be expressed in pyasn1 like this:

class Record(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.1 SET is similar to Python dict, SET OF to list):

>>> record = Record()
>>> record['id'] = 123
>>> record['room'] = 321
>>> str(record)
Record:
 id=123
 room=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.

>>> from pyasn1.codec.der.encoder import encode
>>> substrate = encode(record)
>>> hexdump(substrate)
00000: 30 07 02 01 7B 80 02 01 41

Conversely, 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.

>>> from pyasn1.codec.der.decoder import decode
>>> received_record, rest_of_substrate = decode(substrate, asn1Spec=Record())
>>>
>>> for field in received_record:
>>>    print('{} is {}'.format(field, received_record[field]))
id is 123
room is 321
house is 0
>>>
>>> record == received_record
True
>>> received_record.update(room=123)
>>> substrate = encode(received_record)
>>> hexdump(substrate)
00000: 30 06 02 01 7B 80 01 7B

The 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:

>>> from pyasn1.codec.native.encoder import encode
>>> encode(record)
{'id': 123, 'room': 321, 'house': 0}

Or vice-versa -- you can initialize an ASN.1 structure from a tree of Python objects:

>>> from pyasn1.codec.native.decoder import decode
>>> record = decode({'id': 123, 'room': 321, 'house': 0}, asn1Spec=Record())
>>> str(record)
Record:
 id=123
 room=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.

More information on pyasn1 APIs can be found in the documentation; compiled ASN.1 modules for different protocols and file formats could be found in the pyasn1-modules repo.

How to get pyasn1

This project is forked from pyasn1. The pyasn1 package is distributed under terms and conditions of 2-clause BSD license. Source code is freely available as a GitHub repo.

You could pip install pysnmp-pyasn1 or download it from PyPI.

If something does not work as expected, open an issue at GitHub or post your question on Stack Overflow.

Copyright (c) 2005-2020, Ilya Etingof. All rights reserved. Copyright (c) 2021- Ryan Faircloth. Copyright (c) 2023, Splunk Inc.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysnmp_pyasn1-1.2.0rc1.tar.gz (285.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysnmp_pyasn1-1.2.0rc1-py3-none-any.whl (102.2 kB view details)

Uploaded Python 3

File details

Details for the file pysnmp_pyasn1-1.2.0rc1.tar.gz.

File metadata

  • Download URL: pysnmp_pyasn1-1.2.0rc1.tar.gz
  • Upload date:
  • Size: 285.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pysnmp_pyasn1-1.2.0rc1.tar.gz
Algorithm Hash digest
SHA256 7ebf143a1c1da7014789484bfcaafddd133a44bdec5309d5313f215cae0ef46a
MD5 0f053884bd42c135a3cd6d460ef4522f
BLAKE2b-256 1233c21d34e9ffcfa6fdecac2cf560082e47ba3b5517a8161666396656047d9e

See more details on using hashes here.

File details

Details for the file pysnmp_pyasn1-1.2.0rc1-py3-none-any.whl.

File metadata

  • Download URL: pysnmp_pyasn1-1.2.0rc1-py3-none-any.whl
  • Upload date:
  • Size: 102.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.9 {"installer":{"name":"uv","version":"0.12.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pysnmp_pyasn1-1.2.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 7fd1b5c45b876c64713e0a0506b379a09e63a5db00c15d071e3a56bb53bfb1ab
MD5 63ad01b61b8045ca61072cd861d126c1
BLAKE2b-256 ad8c2ca5f0ce827f41e4cb83f0011d5a0683d073d843c26a13206859ebde48ed

See more details on using hashes here.

Release history Release notifications | RSS feed

2.0.2

2 files

2.0.1

2 files

2.0.0

2 files

1.3.0

2 files

1.2.0

2 files

This release

1.2.0rc1 This release

2 files

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

0.4.12

2 files

0.4.11

2 files

0.4.10

2 files

0.4.9

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page