Package: dtplib
Data Transfer Protocol Librarydtplib is a cross platform networking library written in Python.Source:https://github.com/WKHAllen/dtplibDependenciescompressdircryptographyrsaInstallation$pipinstalldtplibServerCreating a servers=Server(onRecv=None,onConnect=None,onDisconnect=None,blocking=False,eventBlocking=False,recvDir=None,daemon=True,jsonEncode=False)Create a server object.onRecvis a function that will be called when a packet is received. It takes the following parameters: client socket, data, datatype (0: object, 1: file).onConnectis a function that will be called when a client connects. It takes the following parameters: client socket.onDisconnectis a function that will be called when a client disconnects. It takes the following parameters: client socket.
Ifblockingis True, the start method will block until stopping.
IfeventBlockingis True,onRecv,onConnect, andonDisconnectwill block when called.recvDiris the directory in which files will be put in when received.
Ifdaemonis True, all threads spawned will be daemon threads.
IfjsonEncodeis True, packets will be encoded using json instad of pickle.Starting the servers.start(host=None,port=None)Start the server. Ifhostis not specified, it will default tosocket.gethostname(). Ifportis not specified, it will default to0, which represents an arbitrary unused port.Stopping the servers.stop()Stop the server.Check if the server is servings.serving()Get the server's addresss.getAddr()Get a client's addresss.getClientAddr(conn)Get a client's address, providing the client socket object.Get a list of a server's clientss.getClients()Remove a clients.removeClient(conn)Disconnect a client from the server, providing the client socket object.Send data to a clients.send(data,conn=None)Send data to a client.connis the client socket object to send the data to. Ifconnis None, data is sent to all clients.Send a file or directory to a clients.sendFile(path,conn=None)Send a file or directory to a client.pathis the path to the file or directory.connis the client socket object to send the file or directory to. Ifconnis None, the file or directory is sent to all clients.Server alternativeThe dtplib server object can be used in a with statement.withserver(host,port,*args,**kwargs)ass:passhostis the server IP address.portis the port number.argsandkwargswill be passed to the Server class constructor.ClientCreating a clientc=Client(onRecv=None,onDisconnected=None,blocking=False,eventBlocking=False,recvDir=None,daemon=True,jsonEncode=False)Create a client object.onRecvis a function that will be called when a packet is received. It takes the following parameters: data, datatype (0: object, 1: file).onDisconnectedis a function that will be called when the server disconnects suddenly. It takes no parameters.
Ifblockingis True, the connect method will block until disconnecting.
IfeventBlockingis True,onRecvandonDisconnectedwill block when called.recvDiris the directory in which files will be put in when received.
Ifdaemonis True, all threads spawned will be daemon threads.
IfjsonEncodeis True, packets will be encoded using json instad of pickle.Connecting to a serverc.connect(host,port)Disconnecting from the serverc.disconnect()Check if connected to a serverc.connected()Get the client's addressc.getAddr()Get the remote server's addressc.getServerAddr()Send data to the serverc.send(data)Send data to the server, providing the data to be sent.Send a file or directory to the serverc.sendFile(path)Send a file or directory to the server, providing the path to the file or directory to be sent.Client alternativeThe dtplib client object can be used in a with statement.withclient(host,port,*args,**kwargs)asc:passhostis the server IP address.portis the port number.argsandkwargswill be passed to the Client class constructor.