Skip to content

A library based on asyncore, used to build tcp server/client application communicating each other with customized messages.

License

Notifications You must be signed in to change notification settings

sunjinopensource/asynmsg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

asynmsg

A library based on asyncore, used to build tcp server/client application communicating each other with customized messages.

Examples

Server:

import asynmsg
import logging

logging.basicConfig(level=logging.DEBUG,  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('server')

@asynmsg.with_message_handler_config
class ServerSession(asynmsg.SessionS):
    def send_message(self, msg_id, msg_data):
        """override for adding logs"""
        if super().send_message(msg_id, msg_data):
            logger.info('send %s: %s %s', self.get_remote_address(), msg_id, msg_data)
            return True
        return False

    def handle_message(self, msg_id, msg_data):
        """override for adding logs"""
        logger.info('recv %s: %s %s', self.get_remote_address(), msg_id, msg_data)
        super().handle_message(msg_id, msg_data)

    @asynmsg.message_handler_config('Login')
    def on_Login(self, msg_id, msg_data):
        self.send_message('LoginAck', 'login success')

class Server(asynmsg.Server):
    session_class = ServerSession

server = Server()
server.set_listen_address(('127.0.0.1', 12345))
server.start()
asynmsg.run_forever()

Client:

import asynmsg
import logging

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('client')


@asynmsg.with_message_handler_config
class ClientSession(asynmsg.SessionC):
    def on_opened(self):
        super().on_opened()
        self.send_message('Login', 'test1')

    @asynmsg.message_handler_config('LoginAck')
    def on_LoginAck(self, msg_id, msg_data):
        pass

    def send_message(self, msg_id, msg_data):
        """override for adding logs"""
        if super().send_message(msg_id, msg_data):
            logger.info('send: %s %s', msg_id, msg_data)
            return True
        return False

    def handle_message(self, msg_id, msg_data):
        """override for adding logs"""
        logger.info('recv: %s %s', msg_id, msg_data)
        super().handle_message(msg_id, msg_data)


class Client(asynmsg.ClientInfinite):
    session_class = ClientSession


client = Client()
client.set_connect_address(('127.0.0.1', 12345))
client.start()
asynmsg.run_forever()

About

A library based on asyncore, used to build tcp server/client application communicating each other with customized messages.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages