Package: tej-protoc
tej-protoc v1tej-protocv1 is a implementation of custom TEJ protocol. It is a full duplex protocol.
This protocol can be used for sending large files and messages faster.Installationpipinstalltej-protocDiagram+-------------------------------------------------------------------+
|                        TEJ Protocol v1                            |
+-------------------------------------------------------------------+
|         Status Byte            |                                  |
|--------------------------------|           Protocol Version       |
|   Status bit  |  Custom status |              8 bits              |
|    (1 bit)    |  (7 bits)      |                                  |
+-------------------------------------------------------------------+
|                         Number of files                           |
|                           (64 bits)                               |
+-------------------------------------------------------------------+
|        'n' File name length    |        'n' Actual file name      |
|         (16 bits)              |     (from 'n' filename length)   |
+-------------------------------------------------------------------+
|        'n' file length         |      'n' Actual file data        |
|        (64 bits)               |      (from 'n' file length)      |
+-------------------------------------------------------------------+
|                  Repeat for 'n' number of files                   |
+-------------------------------------------------------------------+
|         Message length         |           Message data           |
|           (64 bits)            |       (from message length)      |
+-------------------------------------------------------------------+Status byteThe first bit in the status byte must be1and then remaining bits are the custom status bits.
Custom status bit ranges from0 to 127Example usageServerHere is a simple server implementation with tej-protoc.
Create a new file namedserver.pyfromtej_protoc.serverimportTPServerfromtej_protocimportprotocolfromtej_protocimportcallbacksclassMessageCallback(callbacks.ResponseCallback):defconnected(self,client):builder=protocol.BytesBuilder()builder.set_message(b'Hello')protocol.send(client,builder.bytes())defreceived(self,files,message):print('---- Received in server ----')forfileinfiles:print(file.name)print('Message: ',message.decode())print('---------------------------------')server=TPServer('localhost',8000,MessageCallback)server.listen()You can also access theclientobject inside callback methods withself.client.
Your code inside the Callback class is executed everytime when the client connects and
the data is received. Once the client is connectedconnectedmethod is called. If you want to send data when client
connects, then you can send withprotocol.send(client, builder.build()).To send data from client, you need to build compatible bytes array withBytesBuilderclass.Clientfromtej_protoc.clientimportTPClientfromtej_protocimportprotocolfromtej_protocimportcallbacksclassClientCallback(callbacks.ResponseCallback):defconnected(self,client):builder=protocol.BytesBuilder()builder.set_message(b'Sending from client')# To upload file# builder.add_file('file.txt', open('file.txt', 'rb').read())protocol.send(client,builder.bytes())defreceived(self,files,message):forfileinfiles:print(f'Filename:{file.name}')# Other attributes: file.size, file.dataprint(f'Message:{message.decode()}')deftest_client():try:client=TPClient('localhost',8000,ClientCallback)client.listen()exceptExceptionase:print('error',e)test_client()