Skip to content

Commit 03d40a9

Browse files
committed
Add README and docs.
1 parent 99eb601 commit 03d40a9

3 files changed

Lines changed: 140 additions & 0 deletions

File tree

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# OpenSIPS Python Packages
2+
3+
This repository contains a collection of Python packages for OpenSIPS. These modules are designed to be as lightweight as possible and provide a simple interface for interacting with OpenSIPS.
4+
5+
## Features
6+
7+
Currently, the following packages are available:
8+
- `mi` - can be used to execute OpenSIPS Management Interface (MI) commands.
9+
- `event` - allows you to use OpenSIPS Event Interface subscriptions.
10+
11+
## Usage
12+
13+
1. Install the package from source code:
14+
15+
```bash
16+
git clone
17+
cd python-opebsips
18+
pip install .
19+
```
20+
21+
2. Import the package in your Python code:
22+
23+
```python
24+
from opensips.mi import OpenSIPSMI, OpenSIPSMIException
25+
from opensips.event import OpenSIPSEvent, OpenSIPSEventException
26+
```
27+
28+
3. Use the methods provided by the modules:
29+
30+
```python
31+
mi = OpenSIPSMI('http', url='http://localhost:8888/mi')
32+
try:
33+
response = mi.execute('ps')
34+
# do something with the response
35+
except OpenSIPSMIException as e:
36+
# handle the exception
37+
```
38+
39+
```python
40+
mi_connector = OpenSIPSMI('http', url='http://localhost:8888/mi')
41+
event = OpenSIPSEvent(mi_connector, 'datagram', ip='127.0.0.1', port=50012)
42+
43+
def some_callback(message):
44+
# do something with the message
45+
pass
46+
47+
try:
48+
event.subscribe('E_PIKE_BLOCKED', some_callback)
49+
except OpenSIPSEventException as e:
50+
# handle the exception
51+
52+
try:
53+
event.unsubscribe('E_PIKE_BLOCKED')
54+
except OpenSIPSEventException as e:
55+
# handle the exception
56+
```
57+
58+
## Documentation
59+
60+
* [MI](docs/mi.md) - contains information about supported MI communication types and required parameters for each type.
61+
* [Event Interface](docs/event.md) - lists the supported event transport protocols and provides information about the required parameters for each protocol.
62+
63+
## License
64+
65+
<!-- License source -->
66+
[License-GPLv3]: https://www.gnu.org/licenses/gpl-3.0.en.html "GNU GPLv3"
67+
[Logo-CC_BY]: https://i.creativecommons.org/l/by/4.0/88x31.png "Creative Common Logo"
68+
[License-CC_BY]: https://creativecommons.org/licenses/by/4.0/legalcode "Creative Common License"
69+
70+
The `python-opensips` source code is licensed under the [GNU General Public License v3.0][License-GPLv3]
71+
72+
All documentation files (i.e. `.md` extension) are licensed under the [Creative Common License 4.0][License-CC_BY]
73+
74+
![Creative Common Logo][Logo-CC_BY]
75+
76+
© 2024 - OpenSIPS Solutions

docs/event.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# OpenSIPS Python Packages - Event Interface
2+
3+
This package can be used to subscribe to OpenSIPS Event Interface events.
4+
5+
## Supported backend protocols
6+
7+
The following event transport protocols are supported:
8+
* `datagram` - uses either UDP or UNIX datagram to receive notifications for subscribed events. If using UDP, the `ip` and `port` parameters are required. If using UNIX datagram, the `socket_path` parameter is required.
9+
* `stream` - uses TCP to communicate with the Event Interface. Requires the `ip` and `port` parameters to be set.
10+
11+
## How to use
12+
13+
To instantiate the `OpenSIPSEvent` class, you need to provide a MI connector, the backend protocol and the required parameters in a key-value format. Then `subscribe` and `unsubscribe` methods can be used to manage the subscriptions.
14+
15+
```python
16+
mi_connector = OpenSIPSMI('http', url='http://localhost:8888/mi')
17+
event = OpenSIPSEvent(mi_connector, 'datagram', ip='127.0.0.1', port=50012)
18+
19+
try:
20+
event.subscribe('E_PIKE_BLOCKED', some_callback)
21+
except OpenSIPSEventException as e:
22+
# handle the exception
23+
24+
try:
25+
event.unsubscribe('E_PIKE_BLOCKED')
26+
except OpenSIPSEventException as e:
27+
# handle the exception
28+
```
29+
30+
## Subscribing
31+
32+
By default, the subscription will be permanent. If you want to set a timeout, you can use the `expires` parameter. The value should be an integer representing the number of seconds the subscription will be active.
33+
34+
## How it works
35+
36+
When subscribing to an event, a new thread is created to listen for notifications. The thread will call the callback function provided when an event is received. When unsubscribing, the thread will be stopped and the socket will be closed if no exceptions occur.

docs/mi.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# OpenSIPS Python Packages - MI
2+
3+
This package can be used to execute OpenSIPS Management Interface (MI) commands.
4+
5+
## Supported Communication Types
6+
7+
The following communication types are supported:
8+
* `http` - uses the HTTP protocol to communicate with the MI interface. Requires the `url` parameter to be set.
9+
* `datagram` - uses the UDP protocol to communicate with the MI interface. Requires the `ip` and `port` parameters to be set.
10+
* `fifo` - uses a FIFO file to communicate with the MI interface. Requires 3 parameters: `fifo_file`, `fifo_file_fallback` and `fifo_reply_dir`.
11+
12+
To instantiate the `OpenSIPSMI` class, you need to provide the communication type and the required parameters in a key-value format. For example:
13+
14+
```python
15+
mi = OpenSIPSMI('http', url='http://localhost:8888/mi')
16+
17+
# or
18+
mi = OpenSIPSMI('datagram', ip='127.0.0.1', port=8080)
19+
20+
# or
21+
mi = OpenSIPSMI('fifo', fifo_file='/tmp/opensips_fifo', fifo_file_fallback='/tmp/opensips_fifo_fallback', fifo_reply_dir='/tmp/opensips/')
22+
```
23+
24+
## Methods
25+
26+
The `OpenSIPSMI` class provides the following methods:
27+
* `execute` - to run an MI command and get the response. If an error occurs, an `OpenSIPSMIException` is raised.
28+
* `valid` - to check if the MI connection is valid. Returns a tuple with a boolean value and a list of error messages.

0 commit comments

Comments
 (0)