6. Python使用Asyncio开发TCP服务器简单案例

1. 说明

在Python中开发TCP/IP服务器有两种方式,一种使用Socket,需要在py文件中引入对应的socket包,这种方式只能执行单项任务;另一种方式使用Asyncio异步编程,可以一次创建多个服务器执行不同的任务。

2. 接口说明

python 复制代码
1. asyncio.start_server(callback,ip,port) # 生成一个异步服务器,调用后会自动传给回调函数两个参数writer和reader
	callback: 回调函数,异步操作均在此函数中执行
	ip: ip地址,可以为空,表明接受任意ip的连接
	port: 端口号
2. serve_forever() # 循环执行,接受连接,直到协程被取消
3. asyncio.wait_for(func,time) # 等待执行
	func: 等待执行结束的函数
	time: 等待时间,可以为None表示无限等待
4. writer.write() # 向客户端发送数据
5. writer.drain() # 发送数据后用于清空套接字,需要和writer.write()一起使用
6. reader.read() # 从客户端读取数据
7. writer.close() # 关闭套接字(同时会关闭协程和当前的服务器)
8. writer.wait_closed() # 等待套接字完全关闭,需要和writer.close()一起使用
9. writer.is_closing() # 判断连接是否是断开状态

3. 简单案例

创建一个tcp服务器,并实现数据的接受和发送

python 复制代码
from datetime import datetime
import asyncio

# 服务器的回调函数
async def script_handle(reader, writer): # reader和writer参数是asyncio.start_server生成异步服务器后自动传入进来的
    while True: # 循环接受数据,直到套接字关闭
        # wait_for等待读取数据,第二个参数为等待时间(None表示无限等待)
        data = await asyncio.wait_for(reader.read(2**10), None)
        if not data:
            print('script client disconnected')
            writer.close() # 关闭套接字
            await writer.wait_closed() # 等待套接字完全关闭
            return
        print("received data: ", data.decode())
		writer.write(data.encode()) # 发送数据
		await writer.drain() # 发送数据后,清空套接字

# 主函数
async def main():
    # 生成一个服务器
    server = await asyncio.start_server(script_handle,
                                        host='',
                                        port=8888)
    # 获取请求连接的客户端信息
    addr = server.sockets[0].getsockname()
    print(f'Serving on {addr}')
    # 处理多个请求,永远执行着调用
    async with server:
        await server.serve_forever()
   
if __name__ == '__main__':
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print(datetime.now().strftime('%Y/%m/%d %H:%M:%S.%f'),
              'script server exit by key')

运行此脚本即可接受客户端的正常连接和信息传输。

相关推荐
Amo Xiang几秒前
Python 常用模块(八):logging模块
python·logging·日志
森哥的歌2 分钟前
Python多线程
python·编程·多线程·并发·threading
luck_me52 分钟前
如何远程执行脚本不留痕迹
linux·运维·服务器
-SGlow-8 分钟前
Linux相关概念和易错知识点(40)(HTML资源交互、网页管理、搜索引擎)
linux·运维·服务器·网络·html·交互
抽风的雨61025 分钟前
【python基础知识】Day26 函数
开发语言·python
因缘而起132 分钟前
【Linux】gcc从源码编译安装,修改源码,验证修改的源码
linux·运维·服务器
Luck_ff08101 小时前
服务器选购指南:从零开始了解服务器
运维·服务器
编程有点难1 小时前
Python训练打卡Day22
开发语言·python·机器学习
天机️灵韵1 小时前
字节开源FlowGram与n8n 技术选型
人工智能·python·开源项目
兮兮能吃能睡1 小时前
Python之with语句
数据库·python