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. JAVA 大数 A+B问题

    A + B Problem II I have a very simple problem for you. Given two integers A and B, your job is to ca ...

  2. C++ GUI Qt4编程(08)-3.2spreadsheet-resource

    1. C++ GUI Qt4编程第三章,图片使用资源机制法. 2. 步骤: 2-1. 在resource文件夹下,新建images文件,存放图片. 2-2. 新建spreadsheet.qrc文件,并 ...

  3. protobuf参考

    https://www.cnblogs.com/chenyangyao/p/5422044.html

  4. golang context 剖析 1.7.4 版本

    1. 内部结构之 - timerCtx . type timerCtx struct { cancelCtx timer *time.Timer // Under cancelCtx.mu. dead ...

  5. filter 静态资源

    package com.itheima.web.filter; import java.io.IOException; import javax.servlet.Filter; import java ...

  6. pulic——function(下载的公共的)

    1. /* * 用途: 对Date的扩展,将 Date 转化为指定格式的String */ // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年( ...

  7. 使用request与正则表达式爬取bangumi动画排行榜

    import json import requests from requests.exceptions import RequestException import re import time d ...

  8. FZU 2221—— RunningMan——————【线性规划】

     Problem 2221 RunningMan Accept: 17    Submit: 52Time Limit: 1000 mSec    Memory Limit : 32768 KB  P ...

  9. 白话SpringCloud | 第六章:Hystrix监控面板及数据聚合(Turbine)

    前言 前面一章,我们讲解了如何整合Hystrix.而在实际情况下,使用了Hystrix的同时,还会对其进行实时的数据监控,反馈各类指标数据.今天我们就将讲解下Hystrix Dashboard和Tur ...

  10. Java学习第十九天

    1:异常(理解) (1)程序出现的不正常的情况. (2)异常的体系 Throwable |--Error 严重问题,我们不处理. |--Exception |--RuntimeException 运行 ...