Skip to main content

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 logging

@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 ClientSession:

from anyd import ClientSession

Use it with address and authkey you used for your daemon:

address = ('localhost', 3000)

with ClientSession(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 endpoit:

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 ClientSession(address) as client:
    try:
        client.commit("echo", 1) # will raise TypeError
    except TypeError as ex:
        print(ex) # 1, <class 'int'>

Features

  • Get to your server's functionality implementation instantly
  • Don't bother with a low-level sockets programming
  • The client for your server comes out of the box and is ready to use

Installation

Install it by running:

pip install anyd

Contribute

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

anyd-0.3.7.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

anyd-0.3.7-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file anyd-0.3.7.tar.gz.

File metadata

  • Download URL: anyd-0.3.7.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.9.6 Linux/5.8.0-1039-azure

File hashes

Hashes for anyd-0.3.7.tar.gz
Algorithm Hash digest
SHA256 86d4a329f2f1a6f9e013ad8f667ecfc67d635b00c34858f15ddf85f2a25cf2c7
MD5 14a5c3ef1ccc086f7fde4edc6d0ae073
BLAKE2b-256 3bae9a423ea9200f254d6c05fd4ae32068bcdf1bd0dafa895dccb5dd648546ed

See more details on using hashes here.

File details

Details for the file anyd-0.3.7-py3-none-any.whl.

File metadata

  • Download URL: anyd-0.3.7-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.9.6 Linux/5.8.0-1039-azure

File hashes

Hashes for anyd-0.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 365d2c9443226f182e6b5a96890ea9b1a65d31becce7c65cac6729c517040117
MD5 b6f5bf697d600370d330282a285bce4f
BLAKE2b-256 357b2b041c65108b1e416f1ecc94d4f3b8b00e939d5344a0410dd1dfb5b1b383

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page