Code Monkey home page Code Monkey logo

python-packetsocket's Introduction

packetsocket

High-level socket interface for non-blocking packet communication.

Github

Description

packetsocket provides a simple, high-level class interface for non-blocking sockets communicating in packet messages. Two types of sockets are implemented: PacketSocketServer and PacketSocketClient. A server can accept incoming client connections, a client can connect to one server. Both inherit the common PacketSocket operations (open, close, update, read, write) from the base class PacketSocket. These methods are thread-safe using an internal lock.

PacketSockets work in non-blocking mode regarding the sending and receiving of socket messages. They do, however, have the option to block while waiting for the lock.

PacketSockets communicate messages as simple packets. They are composed as follows: "<HEADER_PREFIX>" where <HEADER_PREFIX> is the constant bytestring PacketSocket.HEADER_PREFIX identifying the beginning of a new message, stores the size of the body of a fixed length of PacketSocket.HEADER_BYTES bytes, and is the message itself.

Example

from queue import Queue
from threading import Thread

from packetsocket import Server, Client

ADDRESS = ("", 4200)
messages = Queue()
running = True


def runServer(messages, address, running):
    with Server(address) as server:
        while running:
            while not messages.empty():
                msg = messages.get_nowait()
                server.write(msg)

            server.update()

            for sock, msgs in server.read():
                client_name = sock.getpeername()[0]
                for msg in msgs:
                    print(f"[{client_name}] {msg.decode()}")


def runClient(messages, address, running):
    with Client(ADDRESS) as client:
        while running:
            client.update()

            for msg in client.read():
                print(f"[Server] {msg.decode()}")
                client.write(b"<Server Echo> " + msg)


def getInput(messages, running):
    while running:
        inp = input("Send message to client: \n")
        if inp.strip():
            messages.put(inp.encode())
            print("\n")


try:
    Thread(target=getInput, args=(messages, running)).start()
    Thread(target=runServer, args=(messages, ADDRESS, running)).start()
    Thread(target=runClient, args=(messages, ADDRESS, running)).start()
finally:
    running = False

python-packetsocket's People

Contributors

dead-man-walker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.