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的更多相关文章

  1. python协程(yield、asyncio标准库、gevent第三方)、异步的实现

    引言 同步:不同程序单元为了完成某个任务,在执行过程中需靠某种通信方式以协调一致,称这些程序单元是同步执行的. 例如购物系统中更新商品库存,需要用"行锁"作为通信信号,让不同的更新 ...

  2. TCP的client和server的简单连接

    server: import socket as s import threading as t bind_ip = "0.0.0.0" bind_port = 80#配置服务器监 ...

  3. asyncio标准库3 HTTP client example

    import aiohttp import asyncio import async_timeout async def fetch(session, url): async with async_t ...

  4. asyncio标准库6 Threads & Subprocess

    Threads import asyncio def compute_pi(digits): # implementation return 3.14 async def main(loop): di ...

  5. asyncio标准库4 asyncio performance

    性能包括2部分 每秒并发请求数(Number of concurrent requests per second) 每秒请求负载(Request latency in seconds: min/ave ...

  6. asyncio标准库7 Producer/consumer

    使用asyncio.Queue import asyncio import random async def produce(queue, n): for x in range(1, n + 1): ...

  7. asyncio标准库2 Hello Clock

    如何调度协程,并发运行 asyncio.gather方法可以聚合协程or future def gather(*coros_or_futures, loop=None, return_exceptio ...

  8. asyncio标准库1 Hello World

    利用asyncio的event loop,编写和调度协程 coroutine [,kəuru:'ti:n] n. 协程 Simple coroutine(调用1个协程) import asyncio ...

  9. python3 基于tcp 简单client和server

    客户端代码 from socket import * #客户端 client=socket(AF_INET,SOCK_STREAM) #通讯地址 client.connect(('172.18.100 ...

随机推荐

  1. 精神AC合集 2018.4.3

    UESTC炸了,先把看似十分OK(只是过了样例)的代码贴上,修复好后再交上去 594 #include<iostream> #include<algorithm> #inclu ...

  2. UESTC - 618

    #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; const int N = 1e6; typed ...

  3. JPEG 编码

    WIN8. DNJXJ-7XBW8-2378T-X22TX-BKG7J 模板:类的宏,泛型,甜饼切割机 类模板:泛型类: 函数模板:泛型函数 STL standard template Library ...

  4. 【实战】SSL和TLS漏洞验证

    工具下载:git clone https://github.com/drwetter/testssl.sh.git 实验环境:192.168.1.22(bee-box v1.6) 192.168.1. ...

  5. mysql数据库知识

    学而时习之,不亦说乎!                              --<论语> 数据库所有操作的总结.   1.mysql的数据库服务为mysqld.exe   windo ...

  6. android 无线调试 [无需数据线][无需root]

    无线调试首要条件在同一网段,打开开发者模式 1,打开 5555 端口 使用数据线链接手机,在命令窗口执行:adb tcpip 5555 2,adb 链接手机调试 这时无需数据线了,在命令窗口执行:ad ...

  7. sha1.js

    function encodeUTF8(s) { var i, r = [], c, x; for (i = 0; i < s.length; i++) if ((c = s.charCodeA ...

  8. Spark遇到的报错和坑

    1. Java版本不一致,导致启动报错. # 解决方法: 在启动脚本最前边添加系统参数,指定Java版本 export JAVA_HOME=/usr/java/jdk1..0_181-amd64/jr ...

  9. spring框架中由FactoryBean获取JedisCluster实例

    spring配置文件: <bean id="jedisCluster" class="com.pingan.ela.structure.ex.JedisCluste ...

  10. oracle 备份恢复篇(五)---rman 剩下控制文件和spfile

    一,环境准备 ❤ 拥有全量备份文件