Package: pynetmanager
pynetmanagerA FOSS library to simplify writing and handling sockets in Python. Includes INET and INET6 support,bind,listen,accept,connectAPI.DescriptionThis repositiory contains the source code for thepynetmanagerlibrary. This is my second and (hopefully) better networking wrapper library, the first beingpyNetSocket.Improvements overpyNetSocket:Direct IPv6 support (throughAF_INET6)DirectSOCK_RAWsupportSupport functions to generate socket objectsUse of threading to avoid blockingPEP8 complianceInstallationInstall directly frompipusingpip install pynetmanageror download the library from theGitHub repousinggit clone https://github.com/AdityaIyer2k7/pynetmanagerUsage - ScriptingCreating a Network Event CallbackDefining the function:defsome_func(conn,addr,data,queues):'''This function will be called when a network event (binding or connection) occursParameters:- 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:frompynetmanagerimportNetCallbackfrommultiprocessingimportQueuemyQueues=[Queue(),Queue()]callback=NetCallback(func=some_func,queues=myQueues)Running the callback (usingsomeConnandsomeAddr):callback.start(conn=someConn,addr=someAddr,data=None)Running a generic Server (AF_INET, SOCK_STREAM)Setup the server:frompynetmanagerimportSvrManager,deafult_socket_inet_streamserver_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 addressmyBindCallbacks=[bindCallback1,bindCallback2]# Conn Callbacks are called when a client connects to the servermyConnCallbacks=[connCallback1,connCallback2,connCallback3]# Always use '+=' to add two lists# do NOT replace any server.xxxxCallbacks unless you know what you're doingserver.bindCallbacks+=myBindCallbacksserver.connCallbacks+=myConnCallbacksBind the server:# If `listenByDeault` is True, the server starts listening for and# accepting clients as soon as it binds to its addressserver.bind(listenByDefault=False)Listen for clients (Skip if you setlistenByDefaultas True):# This function does not require parametersserver.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 queuesserver.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:frompynetmanagerimportCliManager,deafult_socket_inet_streamclient_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 toclient=CliManager(client_socket,server_address)Assign callbacks:# Conn Callbacks are called when the client connects to the servermyConnCallbacks=[connCallback1,connCallback2,connCallback3]# Always use '+=' to add two lists# do NOT replace any client.xxxxCallbacks unless you know what you're doingclient.connCallbacks+=myConnCallbacksConnect the client:client.bind()To access the queue of received messagesclient.recvQueue# OUTPUT:# <multiprocessing.queues.Queue object at ...>