• python网络编程(一)


    一、Server

    带“会话”类的服务器程序

    asyncore    异步套接字处理程序

    asynchat     asyncore的增强版,chat-style或者命令响应协议

    from asyncore import dispatcher
    from asynchat import async_chat
    import socket, asyncore
    
    PORT = 5005
    
    class ChatSession(async_chat):
    
        def __init__(self, sock):
            async_chat. init (self, sock)
            self.set_terminator("
    ")
            self.data = []
    
        def collect_incoming_data(self, data):
            self.data.append(data)
    
        def found_terminator(self):
            line = ''.join(self.data)
            self.data = []
            # Do something with the line...
            print(line)
    
    class ChatServer(dispatcher):
    
        def __init__(self, port): dispatcher. init (self)
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.set_reuse_addr()
            self.bind(('', port))
            self.listen(5)
            self.sessions = []
    
        def handle_accept(self):
            conn, addr = self.accept()
            self.sessions.append(ChatSession(conn))
    
    if __name__ == '__main__':
        s = ChatServer(PORT)
        try: asyncore.loop()
        except KeyboardInterrupt: print()

    1.用到的类:

    dispathcher类基本上是一个套接字类型;

    async_chat类隐藏了大多数套接字读写操作;

    2.作用

    1)ChatServer的  

    self.sessions = []

    用来保存回话列表 ;

    handle_accpet方法创建了新的ChatSession对象,并且将其追加到会话列表中;

    2)conn, addr = self.accept()

    conn是一个sock

    3)asyncore.loop()

    启动服务器,开始监听端口;

    二、聊天服务器

    class ChatServer(dispatcher):
        """
        A class that receives connections and spawns individual
        sessions. It also handles broadcasts to these sessions.
        """
        def __init__(self, port, name):
            # Standard setup tasks dispatcher. init (self)
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.set_reuse_addr()
            self.bind(('', port))
            self.listen(5)
            self.name = name
            self.sessions = []
    
        def disconnect(self, session):
            self.sessions.remove(session)
    
        def broadcast(self, line):
            for session in self.sessions:
                session.push(line + '
    ')
    
        def handle_accept(self):
            conn, addr = self.accept()
            self.sessions.append(ChatSession(self, conn))   
    
    if __name__ == '__main__':
        s = ChatServer(PORT, NAME)
        try: asyncore.loop()
        except KeyboardInterrupt: print()

    作用:

    1)disconnect(self, session)用来处理客户端断开连接;

    2)push方法:

    session对象是ChatSession(async_chat)类型的,写入数据使用push方法;

    3)self.sessions.append(ChatSession(self, conn))

    self, conn 分别对应ChatSession构造函数server, sock

    4)

    def broadcast(self, line):
    for session in self.sessions:

    “广播”即是遍历会话列表;

  • 相关阅读:
    PHP递归方法实现前序、中序、后序遍历二叉树
    php循环方法实现先序、中序、后序遍历二叉树
    Mac charles 抓取https请求,安装证书后还是显示unknown
    PHP工厂模式
    PHP策略模式2
    PHP单例模式
    PHP 面试知识点整理归纳
    十大迷你iPhone天气应用
    来自极客标签10款最新设计素材-系列十三
    帮助快速生成页面固定显示元素的jQuery插件
  • 原文地址:https://www.cnblogs.com/eniac1946/p/7277858.html
Copyright © 2020-2023  润新知