Skip to main content

PROXY protocol library with asyncio server implementation

Project description

proxy-protocol

PROXY protocol library with asyncio server implementation.

Build Status Coverage Status PyPI PyPI PyPI

Specification

API Documentation

Table of Contents

Install and Usage

$ pip install proxy-protocol

Integration with an asyncio.start_server based server is intended to be extremely simple. Here is an example, which will detect PROXY protocol v1 or v2.

from functools import partial
from proxyprotocol.any import ProxyProtocolAny

async def run(host: str, port: int) -> None:
    pp = ProxyProtocolAny()
    callback = partial(on_connection, pp)
    server = await asyncio.start_server(callback, host, port)
    async with server:
        await server.serve_forever()

async def on_connection(pp: ProxyProtocolAny,
                        reader: StreamReader, writer: StreamWriter) -> None:
    result = await pp.read(reader)
    # ... continue using connection
    # result contains the original connection info

You may also check out the proxyprotocol/echo.py reference implementation. If you configure your proxy to send PROXY protocol to localhost:10007, you can see it in action:

$ proxyprotocol-echo --help
$ proxyprotocol-echo any

Development and Testing

You will need to do some additional setup to develop and test plugins. First off, I suggest activating a venv. Then, install the test requirements and a local link to the proxy-protocol package:

$ pip install -r test/requirements.txt
$ pip install -e .

Run the tests with py.test:

$ py.test

If you intend to create a pull request, you should make sure the full suite of tests run by CI/CD is passing:

$ py.test
$ mypy --strict proxyprotocol test
$ flake8 proxyprotocol test

A py.test run executes both unit and integration tests. The integration tests use mocked sockets to simulate the sending and receiving of commands and responses, and are kept in the test/server/ subdirectory.

Type Hinting

This project makes heavy use of Python's type hinting system, with the intention of a clean run of mypy:

mypy --strict proxyprotocol test

No code contribution will be accepted unless it makes every effort to use type hinting to the extent possible and common in the rest of the codebase.

The MIT License (MIT)

Copyright (c) 2020 Ian Good

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

proxy-protocol-0.1.0a0.tar.gz (10.0 kB view hashes)

Uploaded Source

Built Distribution

proxy_protocol-0.1.0a0-py3-none-any.whl (10.7 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