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 ...
随机推荐
- artDialog不能拖拽的问题
需要引用下面这个文件: https://github.com/aui/artDialog/edit/master/dist/dialog-plus.js
- POJ - 2676 暴搜 注意实现细节
经典sudoku问题 按部就班就好 一定要注意细节 大于1还是大于等于1 r c越界判断 judge时0的特判 blabla居然磨了2个小时 改了很多地方所以实现得有点冗余,反正能A吧 /*H E A ...
- PKI公钥基础设施简介
PKI(Public Key Infrastructure)公钥基础设施是提供公钥加密和数字签名服务的系统或平台,目的是为了管理密钥和证书.一个机构通过采用PKI 框架管理密钥和证书可以建立一个安全的 ...
- Q541 反转字符串 II
给定一个字符串和一个整数 k,你需要对从字符串开头算起的每个 2k 个字符的前k个字符进行反转.如果剩余少于 k 个字符,则将剩余的所有全部反转.如果有小于 2k 但大于或等于 k 个字符,则反转前 ...
- selenium模块的而简单使用
一.seleniu的简单使用 1.简单使用 ''' selenium:基于浏览器的自动化操作模块 通过代码定制一些浏览器自动化操作,然后把该操作作用到浏览器 1.pip install seleniu ...
- PIE SDK 坐标系创建、定义、对比
1. 坐标系创建 1.1 从WKT字符串导入空间参考 ISpatialReference接口是一个任何空间参考对象都实现的接口,它包含了所有空间参考对象都公有的方法和属性,如获得空间参考对 ...
- linux的运行模式
一. 运行模式 运行模式也可以称为运行级别. 在Linux中存在一个进程:init(initialize,初始化),进程id是1 该进程存在一个对应的配置文件:inittab(系统运行级别配置文件,位 ...
- js动态给textarea赋值
document.getElementById("new_analysed_news").value=datas.weatherContent;
- IE Error: '__doPostBack' is undefined 问题解决
突然遇到个很奇怪的BUG,翻页控件,其他浏览器一切正常,IE无法翻页,会提示 '__doPostBack' is undefined 后来搜索发现: [原文發表地址] Bug and Fix: ASP ...
- ife task0003学习笔记(一):JavaScript作用域
在学习JavaScript作用域概念之前,首先要明白几个概念:执行环境.变量对象.作用域链. 一.JavaScript执行环境(execution context): 在<Professiona ...