A small framework for building and using custom Unix daemons.
Project description
Anyd
Anyd is a small framework that will help you build and use any custom Unix daemon process as a server. It will suite your daemon with API accessible over sockets, so you'll be able to query it for runnig your code or transmit the data. Anyd provides you a client out-of-the-box, so you can start working with your daemon instantly.
How-to
Anyd provides you an app to start with:
from anyd import Appd
Define the address for your daemon, for example:
address = ("localhost", 3000)
The daemon process will use it to accept connections. Next, create a daemon app:
appd = Appd(address)
Or you can set it up with authentication for client:
appd = Appd(address, authkey=b"swordfish")
Now, define your API endpoints, using @appd.api
:
@appd.api
def echo(arg: str) -> str:
return arg
Additionally, you can use built-in logger to log something specific:
from anyd import logger
@appd.api
def echo(arg: str) -> str:
logging.info(f"echoing: {arg}")
return arg
This function is now exposed to the client as an API endpoint, and can be executed on request.
You are ready to start the deamon:
appd.start()
That will block the interpreter and you'll see the logging output of your daemon in the terminal:
[INFO] Listening on 127.0.0.1:3000
Let's test it from another shell!
Start from importing Session
:
from sockets_framework import Session
Use it with address and authkey you used for your daemon:
address = ('localhost', 3000)
with Session(address, authkey=b"swordfish") as client:
# you can pass keyword arguments to API request
response = client.commit("echo", arg="hello world!")
# or the positional ones
bob = client.commit("echo", "hello Bob")
# you can query different API endpoints per-session
try:
# Will raise NotImplementedError:
# we didn't defined 'my_func' endpoint on the daemon.
# The daemon will continue working.
client.commit("my_func", "hello")
except NotImplementedError as ex:
print(ex) # NotImplementedError: my_func
print(response) # hello world!
print(bob) # hello Bob
Validators
On the daemon app you may want to define sort of validation logic for some of your endpoints. In this case, you can return an exception as a response to the client. It will be pickled and raised on the client side, so your daemon will stay up and running. Consider simple example with previous endpoint:
def validate_echo(arg: Any):
if not isinstance(arg, str):
return TypeError(f"{arg}, {type(arg)}")
return arg
@appd.api
def echo(arg: str) -> str:
return validate_echo(arg)
The function validate_echo
is not an API endpoint of our daemon, but still its accessible for the daemon to execute it locally.
Now, let's try to query it with wrong data:
with Session(address) as client:
try:
client.commit("echo", 1) # will raise TypeError
except TypeError as ex:
print(ex) # 1, <class 'int'>
Features
- Get to your desired functionality instantly
- Don't bother with a low-level sockets programming
- The client for your daemon comes out of the box and is ready to use
Installation
Install anyd by running:
pip install anyd
Contribute
- Issue Tracker: github.com/anatolio-deb/anyd/issues
- Source Code: github.com/anatolio-deb/anyd
License
The project is licensed under the BSD license.
Project details
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
File details
Details for the file anyd-0.3.3.tar.gz
.
File metadata
- Download URL: anyd-0.3.3.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.9.5 Linux/5.8.0-1033-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ab7b682ac12200dec8539c81573d7994d4a14793bb146b9377714d226cd1461 |
|
MD5 | 62bc3ae6fd8167f987eb27b59cb8f657 |
|
BLAKE2b-256 | 4984c4e76e6fea6bdf2bc42c7409a64191e5d010659086b11ef107b6eec569dc |
File details
Details for the file anyd-0.3.3-py3-none-any.whl
.
File metadata
- Download URL: anyd-0.3.3-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.9.5 Linux/5.8.0-1033-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf5f9f1b40167ef474473ab8be85a58569023aa4fd47418389a7b248ce609855 |
|
MD5 | 4424a7c22d6886932c9343862acfd224 |
|
BLAKE2b-256 | 7d65a36627acd3eca288c867334d028adcee88597b4d8f965998096382886a2a |