Python bindings for Asterisk Manager Interface and Asterisk Gateway Interface
Project description
pystrix
pystrix is a versatile Asterisk interface package for AMI and (Fast)AGI needs. Ivrnet, inc. published it as open source under the LGPLv3, and contributions from all users are welcome.
Overview
pystrix runs on Python 3.9+. It targets Asterisk 1.10+ and provides a rich, easy-to-extend set of bindings for three Asterisk integration protocols:
- AMI (Asterisk Manager Interface) — a persistent TCP connection for controlling and monitoring the whole server. Originate calls, hang them up, read channel status, and react to live events.
- AGI (Asterisk Gateway Interface) — per-call scripting over stdin and stdout. Drive the dialplan of a single call: play audio, collect digits, set variables.
- FastAGI — the same call scripting as AGI, served from a long-running threaded TCP server instead of a process spawned per call.
The package is a toolkit, not a framework. You can drop it into a larger project without adopting an async framework such as Twisted.
This repository is version 1.3.0. The canonical version lives in pystrix/__init__.py. New releases follow <major>.<minor>.<patch>, with a patch release cut for each bug fix.
Compatibility
| pystrix | Python |
|---|---|
| 1.3.x | 3.9 – 3.13 |
| 1.2.0 and earlier | 2.7, 3.4+ (legacy, unmaintained) |
From 1.3.0 on, the requires-python metadata makes pip resolve automatically: a project on an older Python receives 1.2.x. All versions target Asterisk 1.10+.
Installation
From PyPI:
pip install pystrix
From GitHub:
pip install git+https://github.com/IVRTech/pystrix.git#egg=pystrix
Quick start
AGI — script a single call
#!/usr/bin/env python
import pystrix
if __name__ == '__main__':
agi = pystrix.agi.AGI()
agi.execute(pystrix.agi.core.Answer()) # Answer the call
# Play a file; DTMF '1' or '2' interrupts playback
response = agi.execute(
pystrix.agi.core.StreamFile('demo-thanks', escape_digits=('1', '2'))
)
if response: # Playback was interrupted
(dtmf_character, offset) = response
agi.execute(pystrix.agi.core.Hangup()) # Hang up
FastAGI — serve many calls from one process
import re
import pystrix
server = pystrix.agi.FastAGIServer()
def demo_handler(agi, args, kwargs, match, path):
agi.execute(pystrix.agi.core.Answer())
agi.execute(pystrix.agi.core.StreamFile('demo-thanks'))
agi.execute(pystrix.agi.core.Hangup())
server.register_script_handler(re.compile('demo'), demo_handler)
server.register_script_handler(None, demo_handler) # default handler
server.serve_forever()
The FastAGI server sizes its listen backlog from the system SOMAXCONN value, so it absorbs large bursts of simultaneous calls. It reads that value with sysctl, so the server currently runs on Linux and macOS only. AMI and AGI have no such restriction.
AMI — control and monitor the server
import pystrix
manager = pystrix.ami.Manager()
manager.connect('localhost')
# Authenticate with an MD5 challenge to avoid sending the password in plain text
challenge = manager.send_action(pystrix.ami.core.Challenge())
manager.send_action(pystrix.ami.core.Login(
'username', 'password', challenge=challenge.result['Challenge'],
))
# React to events as Asterisk emits them
def on_hangup(event, manager):
print('Channel hung up:', event.get('Channel'))
manager.register_callback(pystrix.ami.core_events.Hangup, on_hangup)
Full, commented examples for all three protocols live in doc/examples/.
Documentation
Online documentation is available at http://pystrix.readthedocs.io/.
Inline documentation is complete and written in reStructuredText, so the source stays readable.
For an architecture overview and contributor guidance, see AGENTS.md.
Contributing
pystrix has no third-party runtime dependencies, so setup is short.
git clone https://github.com/IVRTech/pystrix.git
cd pystrix
python3 -m venv .venv && source .venv/bin/activate
pip install -e '.[test]'
The editable install (-e) means your local edits take effect without reinstalling.
A few things to know before you send a change:
- Run the tests with
pytest. CI runs them across Python 3.9 through 3.13 on every pull request. There is no live-Asterisk integration test, so socket-level behavior is still worth checking against a real server (AMI on port 5038, FastAGI on port 4573). - Lint and format with ruff.
ruff check .andruff format --check .must pass, and CI enforces both. Install the hooks to run them on each commit:pip install pre-commit && pre-commit install. - Target Python 3.9+. The codebase is Python 3 only; the old Python 2 compatibility shims have been removed, so don't reintroduce them.
- Build the docs when you touch them:
pip install -r doc/requirements.txt, thencd doc && make html. - Version bumps go in
pystrix/__init__.py.pyproject.tomlreadsVERSIONfrom there dynamically. - Keep docstrings complete and written in reStructuredText. The reference docs are generated from them.
License
pystrix is licensed under the GNU Lesser General Public License v3 or later (COPYING.LESSER). The LGPL extends the GNU General Public License v3 (COPYING), so both license texts ship with the project.
Credits
Neil Tallim created pystrix, and Ivrnet, inc. funded its initial development. For the full list of contributors, see the contributors page or the commit history.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pystrix-1.3.0.tar.gz.
File metadata
- Download URL: pystrix-1.3.0.tar.gz
- Upload date:
- Size: 78.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61afc0983808aff1b7763bbee5a5d3b746792f5e985168aed7352b8d7fae1c2f
|
|
| MD5 |
ed54e4e01ea7c715069edcb56c7fec55
|
|
| BLAKE2b-256 |
1117b8570a9bb5e8e4c47c4fe8cefcfeabb544a3c0a4b7f3e831438b87260e07
|
Provenance
The following attestation bundles were made for pystrix-1.3.0.tar.gz:
Publisher:
publish.yml on IVRTech/pystrix
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pystrix-1.3.0.tar.gz -
Subject digest:
61afc0983808aff1b7763bbee5a5d3b746792f5e985168aed7352b8d7fae1c2f - Sigstore transparency entry: 1942691675
- Sigstore integration time:
-
Permalink:
IVRTech/pystrix@6fecb957c019e27f543f9b8907554760d05786da -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/IVRTech
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6fecb957c019e27f543f9b8907554760d05786da -
Trigger Event:
release
-
Statement type:
File details
Details for the file pystrix-1.3.0-py3-none-any.whl.
File metadata
- Download URL: pystrix-1.3.0-py3-none-any.whl
- Upload date:
- Size: 83.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77819dea68f7d31eb78a5019f39256122a5e15f113899224fb9374e196ffdfff
|
|
| MD5 |
fb6fe8dc590f85f8e934db2cc502a0b3
|
|
| BLAKE2b-256 |
d11f2e21688d0455fb3bc4ff8872d705dc28a29d0d4f33516cc848344b19b178
|
Provenance
The following attestation bundles were made for pystrix-1.3.0-py3-none-any.whl:
Publisher:
publish.yml on IVRTech/pystrix
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pystrix-1.3.0-py3-none-any.whl -
Subject digest:
77819dea68f7d31eb78a5019f39256122a5e15f113899224fb9374e196ffdfff - Sigstore transparency entry: 1942691826
- Sigstore integration time:
-
Permalink:
IVRTech/pystrix@6fecb957c019e27f543f9b8907554760d05786da -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/IVRTech
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6fecb957c019e27f543f9b8907554760d05786da -
Trigger Event:
release
-
Statement type: