Skip to main content

A higher-level extension of the socket module, with simpler and more efficient usages

Project description

CircleCI Badge Documentation Status Codacy Badge Language grade: Python Downloads

PyPI Version Github Version Github Commits

hisock (HiSock, Hisock)

A higher level extension of Python's built-in socket module, with simpler and more efficient usages.

Documentation

Documentation is located on ReadTheDocs. Some documentation is also located in the code as docstrings, though most are already well-documented on ReadTheDocs.

Installation

Hisock only supports Python versions 3.7 and onwards, due to annotations from the __future__ module.

hisock is available on PyPI here, so it is installable by pip. Just do the following command.

$ python -m pip install hisock (WINDOWS)
  OR
$ pip3 install hisock (MAC/LINUX)

To build the bleeding-edge version of hisock from GitHub, download the repository with your favorite way, cd into it, then type:

$ python -m pip install -e . (WINDOWS)
  OR
$ pip3 install -e . (MAC/LINUX)

Note that you MUST cd into the cloned directory for this to work.

Code of Conduct and Contributing Guidelines:

Code of Conduct

Contributing Guidelines

Examples

hisock utilizes decorators as the core of receiving messages instead of having if statements handling all of the logic. More in-depth examples are located in the examples directory, including a tic-tac-toe game. Here is what a basic server script would look like using hisock:

Server

import sys
import time
import random

from hisock import start_server, get_local_ip

ADDR = get_local_ip()
PORT = 6969

if len(sys.argv) == 2:
    ADDR = sys.argv[1]
if len(sys.argv) == 3:
    PORT = int(sys.argv[2])

print(f"Serving at {ADDR}")
server = start_server((ADDR, PORT))

@server.on("join")
def client_join(client_data):
    print(f"Cool, {client_data.ip_as_str} joined!")
    if client_data['name'] is not None:
        print(f"    - With a sick name \"{client_data.name}\", very cool!")
    if client_data['group'] is not None:
        print(f"    - In a sick group \"{client_data.group}\", cool!")

    print("I'm gonna send them a quick hello message")

    server.send_client(client_data['ip'], "hello_message", str(time.time()).encode())

@server.on("processing1")
def process(client_data, process_request: str):
    print(f"\nAlright, looks like {client_data.ip_as_str} received the hello message, "
          "\nas now they're trying to compute something on the server, because they have "
          "potato computers")
    print("Their processing request is:", process_request)

    for _ in range(process_request.count("randnum")):
        randnum = str(random.randint(1, 100000000))
        process_request = process_request.replace("randnum", randnum, 1)

    result = eval(process_request)  # Insecure, but I'm lazy, so...
    print(f"Cool! The result is {result}! I'mma send it to the client")

    server.send_client(client_data, "something", str(result))


server.start()

Client

import time

from hisock import connect, get_local_ip

server_to_connect = input("Enter server IP to connect to (Press enter for default of your local IP): ")
port = input("Enter Server port number (Press enter for default of 6969): ")

if server_to_connect == '':
    server_to_connect = get_local_ip()

if port == '':
    port = 6969
else:
    port = int(port)

name = input("Name? (Press enter for no name) ")
group = input("Group? (Press enter for no group) ")

print("======================================= ESTABLISHING CONNECTION =======================================")

if name == '':
    name = None
if group == '':
    group = None

client = connect(
    (server_to_connect, port),
    name=name, group=group
)
join_time = time.time()


@client.on("hello_message")
def handle_hello(msg: str):
    print("Thanks, server, for sending a hello, just for me!")
    print(f"Looks like, the message was sent on timestamp {msg}, "
          f"which is just {round(float(msg) - join_time, 6) * 1000} milliseconds since the connection!")
    print("In response, I'm going to send the server a request to do some processing")

    client.send("processing1", b"randnum**2")
    result = client.recv("something", int)

    print(f"WHOAAA! The result is {result}! Thanks server!")

client.start()

Contributors

Special thanks to the following:

  • Sheepy0125, for refactoring the underhood code as well as the API
  • SSS-Says-Snek, for maintaining and developing the library
  • Ankith26, for helping me understand the usages of headers
Copyright SSS-Says-Snek, 2021-present

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

hisock-2.0.2.tar.gz (50.8 kB view details)

Uploaded Source

Built Distribution

hisock-2.0.2-py3-none-any.whl (51.6 kB view details)

Uploaded Python 3

File details

Details for the file hisock-2.0.2.tar.gz.

File metadata

  • Download URL: hisock-2.0.2.tar.gz
  • Upload date:
  • Size: 50.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for hisock-2.0.2.tar.gz
Algorithm Hash digest
SHA256 a6a459dd3a21e8ae101f5340987169952480d3f97b371a33cb94bee413da06ef
MD5 8f2b770b7f47f6e99cacbd5a004b1352
BLAKE2b-256 0ec94d06c6e5dcb96c3946b93e56071e7d8ee82caac5f7aeaa35d7d6d99d74b6

See more details on using hashes here.

File details

Details for the file hisock-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: hisock-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 51.6 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.25.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for hisock-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3545fa604355d13085497b60daa6cf514bcf44000e05e61c1dcd1884e03d49c4
MD5 824ebe1a48ed480dcbdf646172adf46f
BLAKE2b-256 284be6f1e468ae6b61f9852c0edf30b19f9d277fc811266bb614465b4f5f2ab8

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