Skip to main content

A FOSS library to simplify writing and handling sockets in Python. Includes INET and INET6 support, `bind`, `listen`, `accept`, `connect` API.

Project description

pynetmanager

A FOSS library to simplify writing and handling sockets in Python. Includes INET and INET6 support, bind, listen, accept, connect API.

Description

This repositiory contains the source code for the pynetmanager library. This is my second and (hopefully) better networking wrapper library, the first being pyNetSocket.

Improvements over pyNetSocket:

  • Direct IPv6 support (through AF_INET6)
  • Direct SOCK_RAW support
  • Support functions to generate socket objects
  • Use of threading to avoid blocking
  • PEP8 compliance

Installation

Install directly from pip using

pip install pynetmanager

or download the library from the GitHub repo using

git clone https://github.com/AdityaIyer2k7/pynetmanager

Usage - Scripting

Creating a Network Event Callback

Defining the function:

def some_func(conn, addr, data, queues):
    '''
    This function will be called when a network event (binding or connection) occurs
    Parameters:
     - conn: The socket object linked to the event
     - addr: The other side's address
     - data: The miscellaneous data being passed to the function, either Iterable or None
     - queues: An iterable (list or dict) of queues between processes
    '''
    print(f"Address {addr} has connected")

Creating the callback:

from pynetmanager import NetCallback
from multiprocessing import Queue

myQueues = [Queue(), Queue()]
callback = NetCallback(func=some_func, queues=myQueues)

Running the callback (using someConn and someAddr):

callback.start(conn=someConn, addr=someAddr, data=None)

Running a generic Server (AF_INET, SOCK_STREAM)

Setup the server:

from pynetmanager import SvrManager, deafult_socket_inet_stream

server_socket = deafult_socket_inet_stream()
server_address = ("127.0.0.1", 5500)
server = SvrManager(server_socket, server_address)

Assign callbacks:

# Bind Callbacks are called when the server binds to its address
myBindCallbacks = [bindCallback1, bindCallback2]

# Conn Callbacks are called when a client connects to the server
myConnCallbacks = [connCallback1, connCallback2, connCallback3]

# Always use '+=' to add two lists
# do NOT replace any server.xxxxCallbacks unless you know what you're doing
server.bindCallbacks += myBindCallbacks
server.connCallbacks += myConnCallbacks

Bind the server:

# If `listenByDeault` is True, the server starts listening for and 
# accepting clients as soon as it binds to its address
server.bind(listenByDefault=False)

Listen for clients (Skip if you set listenByDefault as True):

# This function does not require parameters
server.listen_and_accept(None, None, None, None)

To list connected clients:

server.clients
# OUTPUT:
# [
#   (<socket.socket ...>, ('127.0.0.1', 65028)),
#   (<socket.socket ...>, ('127.0.0.1', 62157)),
#   ...
#   (conn, addr)
# ]

To access open queues

server.recvQueues
# OUTPUT:
# {
#   (<socket.socket ...>, ('127.0.0.1', 65028)): <multiprocessing.queues.Queue ...>,
#   (<socket.socket ...>, ('127.0.0.1', 62157)): <multiprocessing.queues.Queue ...>,
#   ...
#   (conn, addr): <multiprocessing.queues.Queue ...>,
# }

Running a generic Client (AF_INET, SOCK_STREAM)

Setup the client:

from pynetmanager import CliManager, deafult_socket_inet_stream

client_socket = deafult_socket_inet_stream()
server_address = ("127.0.0.1", 5500) # Note that we use the server's address here since that is what we are connecting to
client = CliManager(client_socket, server_address)

Assign callbacks:

# Conn Callbacks are called when the client connects to the server
myConnCallbacks = [connCallback1, connCallback2, connCallback3]

# Always use '+=' to add two lists
# do NOT replace any client.xxxxCallbacks unless you know what you're doing
client.connCallbacks += myConnCallbacks

Connect the client:

client.bind()

To access the queue of received messages

client.recvQueue
# OUTPUT:
# <multiprocessing.queues.Queue object at ...>

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

pynetmanager-1.0.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

pynetmanager-1.0.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file pynetmanager-1.0.0.tar.gz.

File metadata

  • Download URL: pynetmanager-1.0.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for pynetmanager-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e4069df13b183ecb5a9f83c62f55f46216628753adb43726e39a01e2a1a4daf8
MD5 9ea4f34866054b06183c4ad914d58736
BLAKE2b-256 3981c52febb676d34290ba97dc455471d31ec0ccb4564a554d278775d4c96128

See more details on using hashes here.

File details

Details for the file pynetmanager-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pynetmanager-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e200af5e2f568474d07584549585830517554b090b504284c1feb310c2c1b5b7
MD5 f8a118d439975439f58ed9278d30994c
BLAKE2b-256 282e1f7d27f82e6cd3d9a849d3ce3f393edaf4dc86c08a1047f92f22cbc3782b

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