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. 把磁力下载站改为python系统

    已经一年半载没有写博客了,搞得上来不知道写些什么. 索马里影视下载  WWW.IBMID.COM  现在用的是CENTOS 7 系统, 经历了多次点技术变更.开源版本使用了django网站框架重写,之 ...

  2. POJ:2456 Aggressive cows(z最大化最小值)

    描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000, ...

  3. npm的介绍

    npm使JavaScript开发人员能够轻松地共享和重用代码,并且可以轻松更新你正在共享的代码. 如果你一直在使用JavaScript,你可能已经听说过npm.npm使JavaScript开发人员能够 ...

  4. [转] Java8 日期/时间(Date Time)API指南

    [From] http://www.importnew.com/14140.html Java 8日期/时间( Date/Time)API是开发人员最受追捧的变化之一,Java从一开始就没有对日期时间 ...

  5. Java打包成jar

    若要生成一个名为 cal.jar 的可执行jar文件:(文件名可以是任意合法名字)  (这是我认为简单实用的一种方法,还有很多别的方法在此就不介绍了)  第一 把程序生成的所有字节码文件(即.clas ...

  6. PIE SDK屏幕坐标和地图坐标转换

    1. 功能简介 屏幕坐标和地图坐标转换,就是字面意思,将电脑屏幕的坐标转换为带有空间信息的地图坐标,主要运用PIE SDK地图控件的ToMapPoint()方法,而地图坐标转换为设备坐标(屏幕),用的 ...

  7. python学习12-反射 判断函数与方法(转载)

    一.三个内置函数 1.issubclass(a, b)  判断a类是否是b类的子类 class Foo: pass class Zi(Foo): pass class Sun(Zi): passpri ...

  8. stream3

    import java.util.function.Consumer; import java.util.function.IntConsumer; public class ConsumerTest ...

  9. Activemq API使用(整合spring)

    整合spring之后,主要用的就是org.springframework.jms.core.JmsTemplate的API了,在spring-jms-xxx.jar中. 引入整合需要的jar包: &l ...

  10. GreenPlum 大数据平台--分区表(二)

    01,创建分区表 [PARTITION BY partition_type (column) [SUBPARTITION BY partition_type (column)] [SUBPARTITI ...