Package: simple-protocol
Simple ProtocolThis project was created because I got tired of making socket based protocols over and over for various projects.
This allows for quick creation of a server and client which can send "generic" requests/responses back and forth.
These generic transfers can be customized by inheriting the GenericTxParser or GenericTxBuilder classes.UsageServerTo initialize a SimpleProtocolServer:fromsimpleprotocol.serverimportSimpleProtocolServerserver=SimpleProtocolServer(server_name="Test Server")Without registering any handlers, the server won't be of much use. Registering handlers is simple:fromsimpleprotocol.serverimportSimpleProtocolServerfromsimpleprotocol.tximportGenericRequestParser,GenericTxBuilder# Initialize Serverserver=SimpleProtocolServer(server_name="Test Server")defhandler_func(request:GenericRequestParser)->GenericTxBuilder:returnGenericTxBuilder(status=200,response=request.value)# Register Handler and "VALUE" request headerserver.register_handler("method_name",handler_func)server.register_header("VALUE")server.run_server()As seen here,handler_funcmust return aGenericTxBuilderobject in order for the server to send the response properly.
You also need to register any headers you'd like the server to recognize in your requests. In the above example, we are
using "VALUE" to store the information we are sending to the server, so we register "VALUE" as a valid header.ClientInitializing a SimpleProtocolClient is also easy, although different from the server. Typical usage for the SimpleProtocolClient
looks like this:fromsimpleprotocol.clientimportSimpleProtocolClientfromsimpleprotocol.tximportGenericTxBuilderclassMyClient(SimpleProtocolClient):defsend_message(self,message:str):req=GenericTxBuilder(method="method_name",value=message)returnself._send(req)# Returns GenericResponseParser objectclient=MyClient()res=client.send_message("Hello")print(res.response)# "Hello"The_sendmethod ofSimpleProtocolClientalways returns aGenericResponseParserobject which contains the information
returned from the server.MiddlewareSimpleProtocolServer supports registering middleware that can preprocess your requests. For instance, if you wanted to
implement authentication, you could create middleware to validate an authentication header:fromsimpleprotocol.serverimportSimpleProtocolServerfromsimpleprotocol.tximportGenericRequestParser,GenericTxBuilder# Initialize Serverserver=SimpleProtocolServer(server_name="Test Server")defhandler_func(request:GenericRequestParser)->GenericTxBuilder:returnGenericTxBuilder(status=200,response=request.value)defvalidate_authentication(request:GenericRequestParser):valid_keys=["a","b","cd"]ifhasattr(request,"authentication"):ifrequest.authenticationinvalid_keys:returnrequestelse:returnNonereturnNone# Register Handler and "VALUE" request headerserver.register_handler("hello",handler_func)server.register_middleware(validate_authentication)server.register_header("VALUE")server.run_server()In this example, we:Initialize a serverCreate a handler with method name "hello"Register middleware to validate each requests authentication attributeIf middleware returns a request object, the request is allowed to proceed. If a middleware returns anything other than
a request object, the request is canceled. Middleware can also be used things like modifying a request, logging requests,
access control for specific methods, etc.