python 基础教程 24章 虚拟茶话会 async2
来源:互联网 发布:衣柜实木多层板材知乎 编辑:程序博客网 时间:2024/05/21 03:54
#/usr/bin/python
# *-*coding:utf-8 *-*
"""
#python 基础教程24章 24-4 带有ChatSession类的服务器程序
在python2中运行,python3需要用 encode('utf-8')转码
set.terminator(term)设置要在信道上识别的终止对象
collect_incoming_data(data)在每次从套接字中读取一些数据时被自动调用
found_terminator()当输入的数据流与set_terminator()设置的终止条件匹配时调用
"""
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("\r\n")
self.data = []
def collect_incoming_data(self, data):
self.data.append(data)
def found_terminator(self):
line = ''.join(self.data)
self.data = []
#处理这行数据.在服务器端打印出来
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(' End run')
# *-*coding:utf-8 *-*
"""
#python 基础教程24章 24-4 带有ChatSession类的服务器程序
在python2中运行,python3需要用 encode('utf-8')转码
set.terminator(term)设置要在信道上识别的终止对象
collect_incoming_data(data)在每次从套接字中读取一些数据时被自动调用
found_terminator()当输入的数据流与set_terminator()设置的终止条件匹配时调用
"""
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("\r\n")
self.data = []
def collect_incoming_data(self, data):
self.data.append(data)
def found_terminator(self):
line = ''.join(self.data)
self.data = []
#处理这行数据.在服务器端打印出来
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(' End run')
0 0
- python 基础教程 24章 虚拟茶话会 async2
- python 基础教程 24章 虚拟茶话会 async1
- python 基础教程 24章 虚拟茶话会 async3
- python 基础教程 24章 虚拟茶话会 async4
- python 基础教程 24章 虚拟茶话会 async5
- [Python-*-读书]Python基础教程--虚拟茶话会
- Python基础教程项目(5)虚拟茶话会
- Python基础教程之虚拟茶话会程序分析
- Python项目五: 虚拟茶话会
- python项目练习五:虚拟茶话会
- python项目练习五:虚拟茶话会
- python项目练习五:虚拟茶话会
- 茶话会
- Python基础教程(第三章)
- Python基础教程(第四章)
- Python基础教程(第五章)
- Python基础教程(第三章)
- Python基础教程(第四章)
- 如何启用设置org.slf4j.Logger打印并输出日志
- jquery实现iframe效果
- 暂停系统中任意驱动的时钟--StopIoTimer
- 多态
- 百度登陆窗口的设计
- python 基础教程 24章 虚拟茶话会 async2
- 移除时钟KTIMER和DPC
- javascript中静态方法、实例方法、内部方法和原型的一点见解
- Android广播机制 简介
- 广度优先搜索:迷宫最少步数
- python 基础教程 24章 虚拟茶话会 async3
- python 基础教程 24章 虚拟茶话会 async4
- IMWeb小白-百度登录界面
- Cannot proxy target class because CGLIB is not available. Add CGLIB to the class path or specify pr