Skip to main content

User-friendly lib for sockets in Python

Project description

Kangaroo Sockets 🦘

Unit Tests codecov Maintainability Release pre-commit code style: black License

Kangaroo is a user-friendly lib for sockets in Python. You can send and listen to TCP sockets with a few lines of code.

Contents

Installation

First you need Python installed (version 3.6+ is required), then you can install Kangaroo:

$ pip install Kangaroo

Import it ib your code:

import Kangaroo

(Optional) install Jaguar for testing the sockets:

$ brew tap leozz37/jaguar

$ brew install jaguar

Quick start

Sample code for sending and listening to a port:

from src.kangaroo import Kangaroo
import threading
import time


def listen_port(port: int):
    r = Kangaroo().listen(port)

    while True:
        if r.has_new_message():
            print(r.get_message())


if __name__ == '__main__':
    x = threading.Thread(target=listen_port, args=(3000,))
    y = threading.Thread(target=listen_port, args=(3001,))

    x.start()
    y.start()

    while True:
        Kangaroo().send(3000, "Hello")
        Kangaroo().send(3001, "World")
        time.sleep(1)

Documentation

The library consists on two features: listen and send to a given port. You can check the full documentation on pypi.


Listen

Receives a port as int and returns a Kangaroo instance.

def listen(self, port: int):

Usage example:

kangaroo = Kangaroo()

r = kangaroo.listen(3000)
l = kangaroo.listen(3001)

Send

Receives a port and a message, both as string.

def send(self, port: int, message: str) -> None:

Usage example:

kangaroo = Kangaroo()

r = kangaroo.listen(3000)
kangaroo.send(3000, "Hello, World!")

Messages

has_new_messages() returns a bool if there's a new message:

def has_new_message(self) -> bool:

get_message() returns the last message as str:

def get_message(self) -> str:

Usage example:

import Kangaroo


if __name__ == '__main__':
    kangaroo = Kangaroo()

    r = kangaroo.listen(3000)
    kangaroo.send(3000, "Hello world")

    if r.has_new_message():
        print(r.get_message())

Development

This project uses pipenv and pre-commit in order to run some static checks and formatting on the code. After clone the repository you need to create a new virtual environment and install the dependencies:

$ pipenv shell

$ pipenv install --dev --skip-lock

$ pre-commit install

Every time you run the git commit command the code will be checked. To run the checking manually, run:

$ pre-commit run --all-files

Testing

The tests uses the pytest framework. To run the test suit with coverage you can do the following:

$ pytest --cov=. -v

============================================================================================================================ test session starts ============================================================================================================================
platform darwin -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 -- /Library/Developer/CommandLineTools/usr/bin/python3
cachedir: .pytest_cache
rootdir: /Users/leo/Documents/codes/kangaroo
plugins: cov-2.10.1
collected 4 items

tests/kangaroo_test.py::test_send_with_success PASSED                                                                                                                                                                                                                 [ 25%]
tests/kangaroo_test.py::test_listen_with_success PASSED                                                                                                                                                                                                               [ 50%]
tests/kangaroo_test.py::test_get_message_fails PASSED                                                                                                                                                                                                                 [ 75%]
tests/kangaroo_test.py::test_has_new_message_fails PASSED                                                                                                                                                                                                             [100%]

---------- coverage: platform darwin, python 3.8.2-final-0 -----------
Name                     Stmts   Miss  Cover
--------------------------------------------
__init__.py                  3      0   100%
setup.py                     4      4     0%
src/__init__.py              0      0   100%
src/kangaroo.py             31      0   100%
tests/__init__.py            0      0   100%
tests/kangaroo_test.py      23      0   100%
--------------------------------------------
TOTAL                       61      4    93%

Contributing

A full guideline about contributing to Kangaroo can be found in the CONTRIBUTING.md file.

License

Hare is released under the MIT License.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

kangaroo-sockets-0.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distributions

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

kangaroo_sockets-0.1-py3.9.egg (7.3 kB view details)

Uploaded Egg

kangaroo_sockets-0.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file kangaroo-sockets-0.1.tar.gz.

File metadata

  • Download URL: kangaroo-sockets-0.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for kangaroo-sockets-0.1.tar.gz
Algorithm Hash digest
SHA256 60a199e21a95e994d7daa8865fab55ec52b44ae605503d6a1351068759e5f682
MD5 46317c99ccd89ab80e896137c526e75a
BLAKE2b-256 93d796122acb995d8ff19bdbb04fb144712986d24c3ce51c062a1ae46930a37f

See more details on using hashes here.

File details

Details for the file kangaroo_sockets-0.1-py3.9.egg.

File metadata

  • Download URL: kangaroo_sockets-0.1-py3.9.egg
  • Upload date:
  • Size: 7.3 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for kangaroo_sockets-0.1-py3.9.egg
Algorithm Hash digest
SHA256 117762d7382cbe428e5b178a950cce6dd4fef293134a238bfbf63c07d8f802e7
MD5 71041ff38bef7986fb96b7b8e5ca8156
BLAKE2b-256 633171ba36eacda77aab4966884a679cecbddb50335c4fda468e6424fe0cfa43

See more details on using hashes here.

File details

Details for the file kangaroo_sockets-0.1-py3-none-any.whl.

File metadata

  • Download URL: kangaroo_sockets-0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for kangaroo_sockets-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f581aa94e4a89e7187e225685af27c482b18a8a13af7ad081dbca1d8d849a1b
MD5 a030d428b839d7360e7b2c52351e1ae2
BLAKE2b-256 6619221d2ec680025f87b295ff897a9f649f9d81f6197d48e9c3fc703caa518c

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