Package: oon
OONObjects Over Network protocolOON - module for python, which allows you to quickly and conveniently transfer objects of python classes over networkinstall:install python oon module with pip:pip3 install oonusage:OON is able to automaticly convert objects of your custom classes to dicts and strings and to form network message for transfering itIt can act like a server or a client depends on what you needFor example let's assume your python programs with some data related to cars and you have sepparate file cars.py with classes for different cars:################# cars.py ###################
import enum
class CarMark(enum.Enum):
    Volvo = 0
    Toyota = 1
    Ford = 2

class Car:
    mark : CarMark = None
    speed : int = None

class TrucTrailer:
    car_inside : Car = None
    car_count : int = None

class Truc(Car):
    trailer : TrucTrailer = None

class VolvoCar(Car):
    __slots__ = ['wheels']
    mark = CarMark.Volvo
    def __init__(self, speed = 10, wheels = 4):
        self.speed = speed
        self.wheels = wheels
    def show_info(self):
        print(f"VolvoCar\n wheels:{self.wheels} speed:{self.speed}, mark:{self.mark}")

class FordTruck(Truc):
    mark = CarMark.Ford
    def __init__(self, speed = 10, car : Car = None, count = 0):
        self.trailer = TrucTrailer()
        self.speed = speed
        self.trailer.car_inside = car
        self.trailer.car_count = count
    def show_info(self):
        print(f"FordTruck\n speed:{self.speed}, mark:{self.mark}, cars inside trailer:{self.trailer.car_count}")
        print(f"car inside trailer info:")
        self.trailer.car_inside.show_info()So you also have main.py file where all other work done:################# main.py ###################
import cars    #import file with all classes defenitions

my_car = cars.VolvoCar(speed=35, wheels=4)
my_truck = cars.FordTruck(speed=20, car=my_car, count=1)
print("\n")
my_truck.show_info()   #show info about car
print("\n")Now you want to transfermy_truckobject to other host. Let's assume this side will be server and other - clientTo do this - just import oon and turn it on with server mode, wait for client connection and send yourmy_truckobject:Note: ALL FIELDS IN YOUR CLASSES MUST HAVE DEFAULT VALUESalso fields of type list, dict, tuple which contain several objects of other classes does not supported YET################# new main.py - now server side ###################
import oon     #import objects over network protocol
import cars

my_car = cars.VolvoCar(speed=35, wheels=4)
my_truck = cars.FordTruck(speed=20, car=my_car, count=1)
print("\n")
my_truck.show_info()
print("\n")

exitcode = oon.turn_on(modules=[cars], is_server=True)   #turn on oon - returns ExCode
if exitcode != oon.ExCode.Success:
    print(f"failed to start server! {exitcode}")
    exit(1)

client, exitcode = oon.accept_net_connection()   #wait for client connection - returns object of _NetClient
print(f"client with ip {client.addr}")
if exitcode != oon.ExCode.Success:
    print(f"something wrong with connection or timeouted! {exitcode}")
    exit(1)

netmessage, exitcode = oon.generate_net_message(my_truck)   #generate network message from your object - returns _NetMessage and ExCode
if exitcode != oon.ExCode.Success:
    print(f"failed to generate network message! {exitcode}")

exitcode = oon.send_data(netmessage, client)    #send generated message - returns ExCode
if exitcode != oon.ExCode.Success:
    print(f"failed to transfer data! {exitcode}")

oon.close_client_connection(client)    # close connection with client
client = None
exitcode = oon.turn_off()      #turn server off
if exitcode == oon.ExCode.Success:
    print("server is off")
else: print("failed to stop server")now you can createclient.pywhich will be client (it also needs cars.py file):################# client.py ###################
import oon
import cars

exitcode = oon.turn_on(modules=[cars], is_server=False)

if exitcode != oon.ExCode.Success:
    print(f"failed to start client! {exitcode}")
    exit(1)

exitcode = oon.connect_to_srv()
if exitcode != oon.ExCode.Success:
    print(f"failed to connect to srv! {exitcode}")
    exit(1)

netmessage, exitcode = oon.receive_data(bytes=1024)
car = netmessage.netobj
print("\n")
car.show_info()
print("\n")now to test - startpython main.pyandpython client.pyon server:FordTruck
 speed:20, mark:CarMark.Ford, cars inside trailer:1
car inside trailer info:
VolvoCar
wheels:4 speed:35, mark:CarMark.Volvo


client with ip 127.0.0.1
server is offon client:FordTruck
 speed:20, mark:CarMark.Ford, cars inside trailer:1
car inside trailer info:
VolvoCar
wheels:4 speed:35, mark:CarMark.Volvodocs:info about all supported data types, all available functions, returns and objects:all functions:FunctionParametersPurposeCondition to UseReturnturn_on()modules : list- list of modules with your custom classes,classes : list- list of your custom unit classes which aren't located in your modules,is_server : bool- start as server or as client,ip : str- ip,port : int- port,encoding : str- what encoding to use,timeout : int- timeout of future operations,queue_size : int- size of connections in queuestart oonif not startedExCode.SuccessorExCode.StartFailturn_off()noturn off oonif started and if no clients active if in server modeExCode.SuccessorExCode.StopFailIf StopFail on server mode - maybe some clients are still active, close them before stoppingaccept_net_connection()client_timeout : int- timeout of future operations with client connectionwait for client connection until timeoutif started in server mode(_NetClient, ExCode.Success)- if all ok(None, ExCode.BadConn)- if something wrong with listening socket(None, ExCode.Timeout)- if timeouted(None, ExCode.StartFail)- if you forgot to start oonclose_client_connection()client : _NetClient- clientclose connection with clientif started in server mode(_NetClient, ExCode.Success)- if all ok(None, ExCode.BadConn)- if something wrong with client(None, ExCode.StartFail)- if you forgot to start oonconnect_to_srv()noconnect to serverif started in client mode and not connectedExCode.Success- if all okExCode.BadConn- if something wrong with connection, or already connectedExCode.Timeout- if timeoutedExCode.StartFail- if you forgot to start oondisconnect_from_srv()nodisconnect from serverif started in client mode and connectedExCode.Success- if all okExCode.BadConn- if something wrong with connection, or already disconnectedExCode.Timeout- if timeoutedExCode.StartFail- if you forgot to start oongenerate_net_message()netobj : Any - object you want to transfer,fields_to_ignore : list - what fileds of object you don't want to transfer,uuid : str - generated if nothing specifiedgenerate network messageif started(_NetMessage, ExCode.Success)- if all ok(_NetMessage, ExCode.BadData)- if your objects does not meet the requirements(None, ExCode.StartFail)- if you forgot to start oonload_net_message_from_str()messtr : str - json string,fields_to_ignore : list - field not to load from stringload network message from it's json string variant. Uses automatically insidereceive_data()methodif started(_NetMessage, ExCode.Success)- if all ok(_NetMessage, ExCode.BadData)- if something wrong with json string or with your classes in your module(None, ExCode.StartFail)- if you forgot to start oonreceive_data()bytes : int - size of message to expect,client : _NetClient - client. Only if started in server mode!receive data from client or serverif started. If in client mode, needs to be connected to server(_NetMessage, ExCode.Success)- if all ok(_NetMessage, ExCode.BadData)- if failed toload_net_message_from_str()(None, ExCode.BadConn)- if something wrong with connection(None, ExCode.Timeout)- if timeouted(None, ExCode.StartFail)- if you forgot to start oonsend_data()netmessage : _NetMessage - generated network message,client : _NetClient - client. Only if started in server mode!send data to client or serverif started. If in client mode, needs to be connected to serverExCode.Success- if all okExCode.BadData- if you give strange dataExCode.BadConn- if something wrong with clientExCode.Timeout- if timeoutedExCode.StartFail- if you forgot to start oonis_running()noget info about oon statusnoTrue- runningFalse- not runningis_connected()noget info about connect status (Does not tell info about other side connection state!)if started in client modeTrue- connectedFalse- not connectedall objects:_NetMessage (message for transfering between hosts):FieldPurposecreate_code : ExCodesignals if object created as expected. This field returns as exitcode of related functions when generating or loading messagejson_string : strmessage in json string form, this is data that is actually being transferrednetobj : Anyactual object from your custom moduleuuid : struuid of message_NetClient (container for all data related to client connection):FieldPurposealive : Boolif connection wasn't closed manually.This field does not reflect the connection status at the other endsocket : socket.socketclisent connection socketaddr : stripport : intportuuid : struuidusefull info:ExCode.BadConnin most cases means that connection was closed by other side, or you are transmitting wrong data to the functionEvery function argument has default value. You can change it.Note: this module was originally developed as part of a NAM project - https://github.com/Ivashkka/nam