python里使用协程和StreamReader、StreamWriter来创建echo服务端
2017-09-16 10:50
507 查看
Python游戏开发入门
http://edu.csdn.net/course/detail/5690在这个程序里,与前面使用asyncio.Protocol协议不一样,使用底层的StreamReader、StreamWriter来创建。
首先导入asyncio和logging库,接着导入sys库。
定义服务器的IP地址和端口,以便服务器创建监听的端口。
开始定义echo(reader, writer)协程函数,这个函数是当客户端有socket连接过来就会调用这个函数响应,每一个连接过来,就创建一个协程对应工作,所以每个连接是隔离的。
接着配置logging.basicConfig的日志输出。
使用asyncio.get_event_loop()来创建协程的事件循环,使用event_loop.run_forever()进入服务器无限循环。
start_server()创建了一个任务factory返回,接着使用run_until_complete函数运行任务。
这里run_until_complete(server.wait_closed())等所有任务关闭。
源码如下:
import asyncio
import logging
import sys
SERVER_ADDRESS = ('localhost', 10000)
async def echo(reader, writer):
address = writer.get_extra_info('peername')
log = logging.getLogger('echo_{}_{}'.format(*address))
log.debug('connection accepted')
while True:
data = await reader.read(128)
if data:
log.debug('received {!r}'.format(data))
writer.write(data)
await writer.drain()
log.debug('sent {!r}'.format(data))
else:
log.debug('closing')
writer.close()
return
logging.basicConfig(
level=logging.DEBUG,
format='%(name)s: %(message)s',
stream=sys.stderr,
)
log = logging.getLogger('main')
event_loop = asyncio.get_event_loop()
# event_loop.set_debug(True)
# Create the server and let the loop finish the coroutine before
# starting the real event loop.
factory = asyncio.start_server(echo, *SERVER_ADDRESS)
server = event_loop.run_until_complete(factory)
log.debug('starting up on {} port {}'.format(*SERVER_ADDRESS))
# Enter the event loop permanently to handle all connections.
try:
event_loop.run_forever()
except KeyboardInterrupt:
pass
finally:
log.debug('closing server')
server.close()
event_loop.run_until_complete(server.wait_closed())
log.debug('closing event loop')
event_loop.close()
输出如下:asyncio: Using selector: SelectSelector
main: starting up on localhost port 10000
echo_::1_51532: connection accepted
echo_::1_51532: received b'This is the message. It will be sent in parts.'
echo_::1_51532: sent b'This is the message. It will be sent in parts.'
echo_::1_51532: closing
Python游戏开发入门
http://edu.csdn.net/course/detail/5690你也能动手修改C编译器
http://edu.csdn.net/course/detail/5582纸牌游戏开发
http://edu.csdn.net/course/detail/5538
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
RPG游戏从入门到精通
http://edu.csdn.net/course/detail/5246
WiX安装工具的使用
http://edu.csdn.net/course/detail/5207
俄罗斯方块游戏开发
http://edu.csdn.net/course/detail/5110
boost库入门基础
http://edu.csdn.net/course/detail/5029
Arduino入门基础
http://edu.csdn.net/course/detail/4931
Unity5.x游戏基础入门
http://edu.csdn.net/course/detail/4810
TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
TensorFlow入门基本教程
http://edu.csdn.net/course/detail/4369
C++标准模板库从入门到精通
http://edu.csdn.net/course/detail/3324
跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901
跟老菜鸟学python
http://edu.csdn.net/course/detail/2592
在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672
相关文章推荐
- python里使用协程和StreamReader、StreamWriter来创建echo客户端
- python里使用协程来创建echo服务器
- python里使用协程来创建echo客户端
- Python使用MySQLdb创建、调用MySQL存储过程
- Python使用tkinter库创建图形界面HelloWorld
- 使用PYTHON创建XML文档
- 使用python自带的xml.dom创建和解析xml
- 使用 Python 创建 UNIX 命令行工具
- 使用python测测你的系统最多能创建多少个线程
- 使用python测测你的系统最多能创建多少个线程
- 使用JAX-WS创建webservice服务,含服务端及客户端
- 使用 Eclipse 和 Java SE 6 创建独立 Web Services 应用程序,第 1 部分: Web Services 服务端应用程序
- python 使用scapy创建arping脚本
- git服务端、客户端安装、项目创建及常见命令使用
- Fileatream表示文件流,它能够打开和关闭文件,并对文件进行单字节的读写操作。 StreamReader和StreamWriter以文本方式对流进行读写操作。建立一个文本文件,分别使用上面两种方
- StreamWriter和StreamReader的使用
- 在python中使用lambda来创建匿名函数
- 使用Java SE 6 创建独立 Web Services - 服务端
- .net+winform的工程 使用StreamReader和StreamWriter 动态写数据的功能
- python的协程和并发-使用yield实现并有使用详解