Package: gtcp
GTCPSimple and secure TCP frameworkFeaturesTCP server and TCP clientSimple event based messagingSecure automatic RSA encryptionInstallationUsing pip:$pipinstallgtcpIn Python:fromgtcpimportserver,clientDocumentationServerInitializationHandling connectionsSending dataRecieving dataRoomsHandling client disconnectClientInitializationSending dataRecieving dataCallbacksServerInitializationThe server comes as a class. When creating a server with the class, the constructor takes 1 required parameter: the port the server will run on.fromgtcpimportserver# 's' is a TCP server that runs on port 8080s=server(8080)Additionally, the server also takes 1 optional parameter: options. Options should be passed as a dictionary.fromgtcpimportserver# in this case, we are setting the "encrypted" option to trues=server(8080,{"encrypted":True})Handling connectionsServer objects have the.connection()method to handle new client connections. It takes 1 parameter: a callback function with a parameter for the socket.# The socket parameter will be set as an object that represents the connectiondefconnectionhandler(socket):passs.connect(connectionhandler)The socket object is comprised of the IP address of the client, the socket id, the rooms the socket is in, methods for handling and sending data, and methods for handling rooms.defconnectionhandler(socket):print(socket.id)# Output: A random UUIDv4 idprint(socket.ip)# Output: 0.0.0.0print(socket.rooms)# Output: [<the id of the socket>, rooms, the, socket, is, in...]s.connect(connectionhandler)Sending dataTo send data, use the.emit()method. It takes at least two parameters: the event and any amount of data to be sent.You can use the.emit()method with a server object or a socket object to send to all connected sockets or to a single socket respectively.defconnectionhandler(socket):# This will send to the specific socket from the parametersocket.emit("login",username,password)# This will send to all socketss.emit("login",username,password)s.connect(connectionhandler)# This will send to all socketss.emit("login",username,password)Recieving dataTo recieve data from a socket, use it's.on()method. It takes two parameters: the event and a callback function with parameters for all the data.defconnectionhandler(socket):defloginhandler(username,password):passsocket.on("login",loginhandler)s.connect(connectionhandler)RoomsTo do certain interactions with specific sockets or to simple group and organize sockets, use rooms. Rooms are groups of sockets that you can adress seperately from others.To get a socket to join a room, use the.join()method.defconnectionhandler(socket):socket.join("room1")print(socket.rooms)#  Output: [<the id of the socket>, "room1"]s.connect(connectionhandler)To get a socket to leave a room (excluding the room of their own id), use the.leave()method.defconnectionhandler(socket):socket.leave("room1")print(socket.rooms)# Output: [<the id of the socket>]socket.leave(socket.id)print(socket.rooms)# Output: [<the id of the socket>]s.connect(connectionhandler)To get a socket to leave all tooms except for the one is their own id, use the.clearrooms()method.defconnectionhandler(socket):socket.clearrooms()print(socket.rooms)# Output: [<the id of the socket>]s.connect(connectionhandler)To send data to a specific room, use server object's.to()method. It takes one parameter (the room). The.to()methods can be chained to send to multiple rooms.defconnectionhandler(socket):passsocket.onconnection()# This will emit to sockets in room1socket.to("room1").emit("hello","world")# This will emit to both room1 and room2socket.to("room1").to("room2").emit("hello","world")Handling client disconnectTo a socket disconnect, listen with the.on()method for a the "end" event. The callback function takes no parametersdefconnectionhandler(socket):defendhandler():passsocket.on("end",endhandler)s.connect(connectionhandler)ClientInitializationThe client comes as a class. When creating a client with the class, the constructor takes 1 required parameters: the IP address and port of the server to connect to.fromgtcpimportclient# 'c' is a TCP client connected to a server ran on port 8080c=client("localhost:8080")Additionally it takes 2 more optional parameters: a callback function to run when the client connects to the server and an options dictionary.fromgtcpimportclient# 'connectionhandler,' which is optional, is ran when it 'c' finishes connecting to the serverdefclientcallback(c):pass# 'c' is an encrypted TCP client connected to a server ran on port 8080c=client("localhost:8080",{"encrypted":True},clientcallback)Sending dataTo send data to the server, use the.emit()method. It takes at least two parameters: the event and any amount of data to be sent.defclientcallback(c):# This will send to the serverc.emit("login",username,password)c=client("localhost:8080",clientcallback)# This also sends to the serverc.emit("login",username,password)Recieving dataTo recieve data from the server, use the client object's.on()method. It takes two parameters: the event and a callback function with parameters for all the data.defloginhandler(username,password):passdefclientcallback(c):c.on("login",loginhandler)c=client("localhost:8080",clientcallback)# This also worksc.on("login",loginhandler)CallbacksSometimes, it is useful to have a more traditional request-response style API. In GTCP, this is achieved with callback functions.Callback functions are any functions that are sent through an.emit(). The function will be ran and supplied with parameters when it is called by the other side.# In clientdefclientcallback(c):defloginCallback(confirm,userToken=""):ifconfirm:print(userToken)c.emit("login",username,password,loginCallback)c=client("localhost:8080",clientcallback)# In serverdefconnectionhandler(socket):defloginhandler(username,password,callback):ifdb.exists("username",username):ifdb.where("username",username)["password"]==password:callback(1,db.where("username",username)["userToken"])else:callback(0)else:callback(0)socket.on("login",loginhandler)s.connect(connectionhandler)