Skip to main content

A single-threaded server for serving in-memory files via Unix sockets.

Project description

Unix Socket Server

A single-threaded server for serving in-memory files via Unix sockets. No Requirements. just clone this repo and copy unix_socket_server module to the root of your project or install package using pip:

pip install unix-socket-server

Problem

Imagine that we have a in-memory file in our Python application and we want to share this file with other apps or tools on the same host without writing it to disk.

Solution

A lightweight solution to this problem is to use Unix socket communication for file sharing.

Examples:

Use a context manager that automatically manages the server lifecycle:

from unix_socket_server import UnixSocketServerContext

with UnixSocketServerContext(b'hello world', '/tmp/file.sock') as server:
    """
        do stuff
    """

Or you can create and start the server manually.

server = UnixSocketServer(b'Hello World')
server.listen('/tmp/stream.socket')

UnixSocketServer writes file to response stream by default . but if your client needs http protocol you can use http handler like example below:

from unix_socket_server import UnixSocketServerContext
with open('./file-exmaple.txt', 'rb') as f:
    """
    UnixSocketServer also accepts readable io objects
    """
    with UnixSocketServerContext(f, '/tmp/file.sock', handler='http') as server:
        """
            do stuff
            dummy example:
                transport = httpx.HTTPTransport(uds=server.uri)
                client = httpx.Client(transport=transport)
                response = client.get('http://any_path/')
                assert response.status_code == 200
        """

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

unix-socket-server-0.0.5.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

unix_socket_server-0.0.5-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

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