Skip to main content

Common library for spectre libs

Project description

Simple Protocol

This project was created because I got tired of making socket based protocols over and over for various projects. This allows for quick creation of a server and client which can send "generic" requests/responses back and forth. These generic transfers can be customized by inheriting the GenericTxParser or GenericTxBuilder classes.

Usage

Server

To initialize a SimpleProtocolServer:

from simpleprotocol.server import SimpleProtocolServer
server = SimpleProtocolServer(server_name="Test Server")

Without registering any handlers, the server won't be of much use. Registering handlers is simple:

from simpleprotocol.server import SimpleProtocolServer
from simpleprotocol.tx import GenericRequestParser, GenericTxBuilder

# Initialize Server
server = SimpleProtocolServer(server_name="Test Server")

def handler_func(request: GenericRequestParser) -> GenericTxBuilder:
    return GenericTxBuilder(status=200, response=request.value)

# Register Handler and "VALUE" request header
server.register_handler("method_name", handler_func)
server.register_header("VALUE")
server.run_server()

As seen here, handler_func must return a GenericTxBuilder object in order for the server to send the response properly. You also need to register any headers you'd like the server to recognize in your requests. In the above example, we are using "VALUE" to store the information we are sending to the server, so we register "VALUE" as a valid header.

Client

Initializing a SimpleProtocolClient is also easy, although different from the server. Typical usage for the SimpleProtocolClient looks like this:

from simpleprotocol.client import SimpleProtocolClient
from simpleprotocol.tx import GenericTxBuilder

class MyClient(SimpleProtocolClient):
    def send_message(self, message: str):
        req = GenericTxBuilder(method="method_name", value=message)
        return self._send(req) # Returns GenericResponseParser object

client = MyClient()
res = client.send_message("Hello")
print(res.response) # "Hello"

The _send method of SimpleProtocolClient always returns a GenericResponseParser object which contains the information returned from the server.

Middleware

SimpleProtocolServer supports registering middleware that can preprocess your requests. For instance, if you wanted to implement authentication, you could create middleware to validate an authentication header:

from simpleprotocol.server import SimpleProtocolServer
from simpleprotocol.tx import GenericRequestParser, GenericTxBuilder

# Initialize Server
server = SimpleProtocolServer(server_name="Test Server")

def handler_func(request: GenericRequestParser) -> GenericTxBuilder:
    return GenericTxBuilder(status=200, response=request.value)

def validate_authentication(request: GenericRequestParser):
    valid_keys = ["a", "b", "cd"]
    if hasattr(request, "authentication"):
        if request.authentication in valid_keys:
            return request
        else:
            return None
    return None

# Register Handler and "VALUE" request header
server.register_handler("hello", handler_func)
server.register_middleware(validate_authentication)
server.register_header("VALUE")
server.run_server()

In this example, we:

  • Initialize a server
  • Create a handler with method name "hello"
  • Register middleware to validate each requests authentication attribute

If middleware returns a request object, the request is allowed to proceed. If a middleware returns anything other than a request object, the request is canceled. Middleware can also be used things like modifying a request, logging requests, access control for specific methods, etc.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

simple_protocol-0.1.9-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file simple_protocol-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: simple_protocol-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for simple_protocol-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 f4a5c6d295893a421503d7434284c320ef9e4959c4ac5e3d5731b0c75b4d60ca
MD5 ce6d699fd24ba1572ae5f35e7a3366f7
BLAKE2b-256 0198cec886368e24aef2435bcdbe45834ea9f58bd4c4fe98d941c5fb9cf1bdfb

See more details on using hashes here.

Supported by

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