asyncio标准库5 TCP echo client and server
server
import asyncio
async def handle_echo(reader, writer):
data = await reader.read(100)
message = data.decode()
addr = writer.get_extra_info('peername')
print("Received %r from %r" % (message, addr))
print("Send: %r" % message)
writer.write(data)
await writer.drain()
print("Close the client socket")
writer.close()
loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop)
server = loop.run_until_complete(coro)
# Serve requests until Ctrl+C is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()
client
import asyncio
async def tcp_echo_client(message, loop):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888, loop=loop)
print('Send: %r' % message)
writer.write(message.encode())
data = await reader.read(100)
print('Received: %r' % data.decode())
print('Close the socket')
writer.close()
message = 'Hello World!'
loop = asyncio.get_event_loop()
loop.run_until_complete(tcp_echo_client(message, loop))
loop.close()
# server
Serving on ('127.0.0.1', 8888)
Received 'Hello World!' from ('127.0.0.1', 43000)
Send: 'Hello World!'
Close the client socket
Received 'Hello World!' from ('127.0.0.1', 43006)
Send: 'Hello World!'
Close the client socket
Received 'Hello World!' from ('127.0.0.1', 43008)
Send: 'Hello World!'
Close the client socket
Received 'Hello World!' from ('127.0.0.1', 43010)
Send: 'Hello World!'
Close the client socket
Received 'Hello World!' from ('127.0.0.1', 43012)
Send: 'Hello World!'
Close the client socket
# client
Send: 'Hello World!'
Received: 'Hello World!'
Close the socket
Send: 'Hello World!'
Received: 'Hello World!'
Close the socket
....
asyncio标准库5 TCP echo client and server的更多相关文章
- python协程(yield、asyncio标准库、gevent第三方)、异步的实现
引言 同步:不同程序单元为了完成某个任务,在执行过程中需靠某种通信方式以协调一致,称这些程序单元是同步执行的. 例如购物系统中更新商品库存,需要用"行锁"作为通信信号,让不同的更新 ...
- TCP的client和server的简单连接
server: import socket as s import threading as t bind_ip = "0.0.0.0" bind_port = 80#配置服务器监 ...
- asyncio标准库3 HTTP client example
import aiohttp import asyncio import async_timeout async def fetch(session, url): async with async_t ...
- asyncio标准库6 Threads & Subprocess
Threads import asyncio def compute_pi(digits): # implementation return 3.14 async def main(loop): di ...
- asyncio标准库4 asyncio performance
性能包括2部分 每秒并发请求数(Number of concurrent requests per second) 每秒请求负载(Request latency in seconds: min/ave ...
- asyncio标准库7 Producer/consumer
使用asyncio.Queue import asyncio import random async def produce(queue, n): for x in range(1, n + 1): ...
- asyncio标准库2 Hello Clock
如何调度协程,并发运行 asyncio.gather方法可以聚合协程or future def gather(*coros_or_futures, loop=None, return_exceptio ...
- asyncio标准库1 Hello World
利用asyncio的event loop,编写和调度协程 coroutine [,kəuru:'ti:n] n. 协程 Simple coroutine(调用1个协程) import asyncio ...
- python3 基于tcp 简单client和server
客户端代码 from socket import * #客户端 client=socket(AF_INET,SOCK_STREAM) #通讯地址 client.connect(('172.18.100 ...
随机推荐
- [BZOJ 3262]陌上开花
今天写了到偏序问题,发现博主真的是个傻X 传送门 以前的写法 /************************************************************** Probl ...
- [洛谷 P4612][COCI 2011-2012#7] Setnja
传送门 TM :setnja (1S256M) 一个人要散步去会见他的 N 个朋友(按给定的顺序会见).我们可以理解成他们都住在一个 很大的网格内,每个朋友住其中的一个单元格,所有人每一步都可以走到相 ...
- LeetCode记录之14——Longest Common Prefix
本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...
- C. Connect Three Round #528 (Div. 2)【曼哈顿距离】
一.题面 题目链接 二.分析 这题的关键是要确定一个点是从三个点出发的交汇点,其他的只要结合曼哈顿距离的定义即可明白.因为是三个点,这个交汇点的坐标分别对应的就是x,y值的中值.然后一个小技巧就是曼哈 ...
- linux 配置环境变量
配置全局 环境变量 查看环境变量 #这个变量赋值操作,只是临时生效,需要写入到文件,永久生效 echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/u ...
- 可持久化Treap 赛前摸鱼笔记
1.基本结构 随机化工具 unsigned int SEED = 19260817; //+1s inline int Rand(){ SEED=SEED*1103515245+12345; retu ...
- eclipse+maven远程(自动)部署web项目到tomcat
[转自] http://blog.csdn.net/dhmpgt/article/details/11197995 eclipse集成maven后可以用maven命令把web项目自动部署到tomcat ...
- 2019.3.22 SQL语句(基础篇)
SQL语句 创建一个数据库: create database+数据库名; 使用数据库: use+数据库名; 查看mySQL中有哪些数据库: show databases; 删除数据库 drop dat ...
- linux 7.0+救援模式
输入“e”进入GRUB页面! 定位到ro 然后修改ro为rw,并添加如下红框内的命令行! 使用下面的提示使用“Ctrl-x”start!!! 进入系统后,其实只是进入了一个安全模式下的内存系统,并不是 ...
- Maths Intro - Probability
设事件A,B,C两辆独立,且满足ABC=空集,及P(A)=P(B)=P(C)=x,求max(x) x最大值为1/2分析: x值要保证所有的由A.B.C交或并得到的集合的概率测度在0到1之间. 先考虑A ...