python 基础教程 24章 虚拟茶话会 async1
来源:互联网 发布:红颜知已歌曲 编辑:程序博客网 时间:2024/06/03 19:25
#/usr/bin/python
# *-*coding:utf-8 *-*
"""
#python 基础教程 24章 24-3具有一些清理功能的基本服务器
asyncore模块链接:https://docs.python.org/3/library/asyncore.html
asynchat模块链接 https://docs.python.org/3/library/asynchat.html
socket模块链接 https://docs.python.org/3/library/socket.html
asyncore模块中的dispatcher类基本上就是一个套接字对象
create_socket方法创建一个套接字,然后利用bind把服务器绑定到具体的地址上(主机名和端口)
主机名为空(空字符串,意味着本地主机,或者更专业一点来说是 '本机的所有接口')
listen方法监听进入的连接,参数为允许排队等待的连接数目
set_reuse_addr()调用可以在服务器没有正确关闭的情况下重用一个地址(具体来说是端口号)
handle_accept方法会调用允许客户端连接的self.accept函数
它返回一个连接(针对客户端的具体套接字)和一个地址(有关所连接计算机的信息)
loop方法 启动轮询循环监听 asyncore.loop([timeout[, use_poll[, map[, count]]]])
"""
from asyncore import dispatcher
import socket, asyncore
PORT = 5005
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)
def handle_accept(self):
conn, addr = self.accept()
print("Connection attempt from", addr[0])
if __name__ == '__main__':
s = ChatServer(PORT)
try:
asyncore.loop()
except KeyboardInterrupt:pass
# *-*coding:utf-8 *-*
"""
#python 基础教程 24章 24-3具有一些清理功能的基本服务器
asyncore模块链接:https://docs.python.org/3/library/asyncore.html
asynchat模块链接 https://docs.python.org/3/library/asynchat.html
socket模块链接 https://docs.python.org/3/library/socket.html
asyncore模块中的dispatcher类基本上就是一个套接字对象
create_socket方法创建一个套接字,然后利用bind把服务器绑定到具体的地址上(主机名和端口)
主机名为空(空字符串,意味着本地主机,或者更专业一点来说是 '本机的所有接口')
listen方法监听进入的连接,参数为允许排队等待的连接数目
set_reuse_addr()调用可以在服务器没有正确关闭的情况下重用一个地址(具体来说是端口号)
handle_accept方法会调用允许客户端连接的self.accept函数
它返回一个连接(针对客户端的具体套接字)和一个地址(有关所连接计算机的信息)
loop方法 启动轮询循环监听 asyncore.loop([timeout[, use_poll[, map[, count]]]])
"""
from asyncore import dispatcher
import socket, asyncore
PORT = 5005
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)
def handle_accept(self):
conn, addr = self.accept()
print("Connection attempt from", addr[0])
if __name__ == '__main__':
s = ChatServer(PORT)
try:
asyncore.loop()
except KeyboardInterrupt:pass
0 0
- python 基础教程 24章 虚拟茶话会 async1
- python 基础教程 24章 虚拟茶话会 async2
- python 基础教程 24章 虚拟茶话会 async3
- python 基础教程 24章 虚拟茶话会 async4
- python 基础教程 24章 虚拟茶话会 async5
- [Python-*-读书]Python基础教程--虚拟茶话会
- Python基础教程项目(5)虚拟茶话会
- Python基础教程之虚拟茶话会程序分析
- Python项目五: 虚拟茶话会
- python项目练习五:虚拟茶话会
- python项目练习五:虚拟茶话会
- python项目练习五:虚拟茶话会
- 茶话会
- Python基础教程(第三章)
- Python基础教程(第四章)
- Python基础教程(第五章)
- Python基础教程(第三章)
- Python基础教程(第四章)
- 2. CMake-------Multi source documents coding
- java环境配置——java能运行 javac 不是内部命令和外部命令 注意事项!
- 153_Collection集合的常用方法详解_01
- 在虚拟机(CentOS)上配置fastdfs+nginx
- 插入排序之shell排序分析及源码演示
- python 基础教程 24章 虚拟茶话会 async1
- 如何启用设置org.slf4j.Logger打印并输出日志
- jquery实现iframe效果
- 暂停系统中任意驱动的时钟--StopIoTimer
- 多态
- 百度登陆窗口的设计
- python 基础教程 24章 虚拟茶话会 async2
- 移除时钟KTIMER和DPC
- javascript中静态方法、实例方法、内部方法和原型的一点见解