当前位置: 首页 > news >正文

网站设计制作哪种快教育培训机构排名

网站设计制作哪种快,教育培训机构排名,wordpress php调优,线上购物平台异步套接字编程是异步编程在网络通信中的应用,它使用异步 IO 操作和事件循环来实现高并发的网络应用。Python 中的 asyncio 模块提供了对异步套接字编程的支持,以下是异步套接字编程的一些重要概念和使用方法: 1. 异步套接字服务器&#xff…

异步套接字编程是异步编程在网络通信中的应用,它使用异步 IO 操作和事件循环来实现高并发的网络应用。Python 中的 asyncio 模块提供了对异步套接字编程的支持,以下是异步套接字编程的一些重要概念和使用方法:

1. 异步套接字服务器:

异步套接字服务器通过 asyncio.start_server() 函数创建,该函数返回一个 asyncio.Server 对象,它是一个异步迭代器,可以在事件循环中进行迭代。

import asyncioasync def handle_client(reader, writer):data = await reader.read(100)message = data.decode()addr = writer.get_extra_info('peername')print(f"Received {message} from {addr}")print("Send: %r" % message)writer.write(data)await writer.drain()print("Closing the connection")writer.close()async def main():server = await asyncio.start_server(handle_client, '127.0.0.1', 8888)addr = server.sockets[0].getsockname()print(f'Serving on {addr}')async with server:await server.serve_forever()asyncio.run(main())

在上述代码中,handle_client 函数是一个协程,用于处理客户端连接。asyncio.start_server() 创建一个异步套接字服务器,监听指定的地址和端口。通过 await server.serve_forever() 使服务器一直运行。

2. 异步套接字客户端:

异步套接字客户端使用 asyncio.open_connection() 函数创建,返回一个由 (reader, writer) 组成的元组。

import asyncioasync def send_message(message):reader, writer = await asyncio.open_connection('127.0.0.1', 8888)print(f'Send: {message!r}')writer.write(message.encode())data = await reader.read(100)print(f'Received: {data.decode()!r}')print('Closing the connection')writer.close()asyncio.run(send_message("Hello, server!"))

在上述代码中,send_message 函数是一个协程,使用 asyncio.open_connection() 函数创建一个异步套接字客户端。通过协程中的异步 IO 操作实现数据的发送和接收。

3. 异步套接字的异常处理:

在异步套接字编程中,需要特别关注异常的处理。例如,在服务器中,可能需要处理客户端连接中断的情况:

async def handle_client(reader, writer):try:data = await reader.read(100)# 处理数据except asyncio.CancelledError:print("Client connection was cancelled.")except Exception as e:print(f"An error occurred: {e}")finally:writer.close()async def main():server = await asyncio.start_server(handle_client, '127.0.0.1', 8888)addr = server.sockets[0].getsockname()print(f'Serving on {addr}')async with server:await server.serve_forever()asyncio.run(main())

4. 异步套接字的超时处理:

在异步套接字编程中,有时需要设置超时时间,以防止长时间等待某个操作完成。可以使用 asyncio.wait_for() 函数来设置超时。

import asyncioasync def send_message_with_timeout(message):try:reader, writer = await asyncio.open_connection('127.0.0.1', 8888)await asyncio.wait_for(writer.write(message.encode()), timeout=5)data = await asyncio.wait_for(reader.read(100), timeout=5)print(f'Received: {data.decode()!r}')except asyncio.TimeoutError:print("Operation timed out.")except Exception as e:print(f"An error occurred: {e}")finally:writer.close()asyncio.run(send_message_with_timeout("Hello, server!"))

5. 异步套接字编程中的并发处理:

异步套接字编程充分利用事件循环和协程的特性,可以在单个线程中有效地处理大量并发连接。这通过异步 IO 操作的非阻塞特性实现。以下是一个简单的示例,展示如何在异步套接字服务器中处理多个并发连接:  //handle_client 函数是一个协程,用于处理单个客户端连接。由于协程的非阻塞特性,事件循环可以同时处理多个连接,而不会阻塞等待 IO 操作完成。

import asyncioasync def handle_client(reader, writer):try:data = await reader.read(100)message = data.decode()addr = writer.get_extra_info('peername')print(f"Received {message} from {addr}")print("Send: %r" % message)writer.write(data)await writer.drain()print("Closing the connection")except asyncio.CancelledError:print("Client connection was cancelled.")except Exception as e:print(f"An error occurred: {e}")finally:writer.close()async def main():server = await asyncio.start_server(handle_client, '127.0.0.1', 8888)addr = server.sockets[0].getsockname()print(f'Serving on {addr}')async with server:await server.serve_forever()asyncio.run(main())

6. 高级概念 - SSL/TLS 加密:

异步套接字编程也支持通过 SSL/TLS 进行安全的加密通信。可以使用 asyncio.start_server()asyncio.open_connection()ssl 参数来实现。

以下是一个简单的示例,演示了如何在异步套接字服务器和客户端中使用 SSL/TLS 加密:

 

import asyncio
import sslasync def handle_client(reader, writer):# 处理客户端连接# ...async def main():# 服务器端 SSL/TLS 设置ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)ssl_context.load_cert_chain(certfile='server.crt', keyfile='server.key')server = await asyncio.start_server(handle_client, '127.0.0.1', 8888, ssl=ssl_context)addr = server.sockets[0].getsockname()print(f'Serving on {addr}')async with server:await server.serve_forever()asyncio.run(main())

在客户端中也可以使用 ssl 参数,通过 ssl.create_default_context() 创建 SSL/TLS 上下文。

import asyncio
import sslasync def send_message(message):# 客户端 SSL/TLS 设置ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)ssl_context.load_verify_locations('server.crt')reader, writer = await asyncio.open_connection('127.0.0.1', 8888, ssl=ssl_context)# 发送和接收数据# ...asyncio.run(send_message("Hello, server!"))

7.总结:

异步套接字编程通过充分利用异步 IO 操作和事件循环的特性,可以在网络编程中实现高效的并发处理。这使得程序能够有效地处理大量的并发连接,提高了性能和资源利用率。在编写异步套接字编程代码时,需要关注异常处理、超时处理以及其他安全性和性能方面的考虑。


文章转载自:
http://depose.rdgb.cn
http://argentite.rdgb.cn
http://labilize.rdgb.cn
http://neuroleptanalgesia.rdgb.cn
http://mucilage.rdgb.cn
http://cheapo.rdgb.cn
http://bromelia.rdgb.cn
http://orismology.rdgb.cn
http://turdine.rdgb.cn
http://stigmatism.rdgb.cn
http://bubo.rdgb.cn
http://ambidextrous.rdgb.cn
http://sarcina.rdgb.cn
http://jurancon.rdgb.cn
http://footnote.rdgb.cn
http://nurserymaid.rdgb.cn
http://straitlace.rdgb.cn
http://cubicule.rdgb.cn
http://schoolyard.rdgb.cn
http://amphidromia.rdgb.cn
http://posthouse.rdgb.cn
http://magicube.rdgb.cn
http://rhinencephalic.rdgb.cn
http://dandyprat.rdgb.cn
http://panmunjom.rdgb.cn
http://nonfreezing.rdgb.cn
http://mnemotechnist.rdgb.cn
http://grammaticus.rdgb.cn
http://uproarious.rdgb.cn
http://milon.rdgb.cn
http://febricide.rdgb.cn
http://absentminded.rdgb.cn
http://chadian.rdgb.cn
http://clearcole.rdgb.cn
http://loathful.rdgb.cn
http://someways.rdgb.cn
http://stereography.rdgb.cn
http://snakish.rdgb.cn
http://dihydroxyacetone.rdgb.cn
http://salud.rdgb.cn
http://decelerometer.rdgb.cn
http://pholas.rdgb.cn
http://quai.rdgb.cn
http://pigmentation.rdgb.cn
http://semiliteracy.rdgb.cn
http://auspicial.rdgb.cn
http://gangtok.rdgb.cn
http://girdler.rdgb.cn
http://metalanguage.rdgb.cn
http://historic.rdgb.cn
http://jumbal.rdgb.cn
http://curtain.rdgb.cn
http://fusuma.rdgb.cn
http://denudate.rdgb.cn
http://duvetine.rdgb.cn
http://cytopathogenic.rdgb.cn
http://rio.rdgb.cn
http://speakerphone.rdgb.cn
http://overstory.rdgb.cn
http://mazdoor.rdgb.cn
http://disarm.rdgb.cn
http://chloroform.rdgb.cn
http://masty.rdgb.cn
http://pavin.rdgb.cn
http://seedeater.rdgb.cn
http://adat.rdgb.cn
http://audiotape.rdgb.cn
http://periscope.rdgb.cn
http://maoritanga.rdgb.cn
http://singaporean.rdgb.cn
http://eunuchoid.rdgb.cn
http://kkk.rdgb.cn
http://manageability.rdgb.cn
http://lalophobia.rdgb.cn
http://checkered.rdgb.cn
http://roughneck.rdgb.cn
http://razzle.rdgb.cn
http://necessitarian.rdgb.cn
http://bucker.rdgb.cn
http://kos.rdgb.cn
http://urological.rdgb.cn
http://ordinance.rdgb.cn
http://shooting.rdgb.cn
http://histogenesis.rdgb.cn
http://salometer.rdgb.cn
http://anciently.rdgb.cn
http://amerasian.rdgb.cn
http://expensive.rdgb.cn
http://turco.rdgb.cn
http://dermopteran.rdgb.cn
http://ballottement.rdgb.cn
http://pyramidal.rdgb.cn
http://phospholipide.rdgb.cn
http://dingo.rdgb.cn
http://frequentist.rdgb.cn
http://splenectomize.rdgb.cn
http://catenation.rdgb.cn
http://cloudland.rdgb.cn
http://panbroil.rdgb.cn
http://collapsible.rdgb.cn
http://www.hrbkazy.com/news/72716.html

相关文章:

  • 一级a做爰片免费网站 小说企业站seo案例分析
  • 网上去哪里找做网站的中国十大知名网站
  • 做网站电商云数据库有用吗互联网行业都有哪些工作
  • 贵州省建设银行网站电商线上推广
  • 德国网站域名后缀nba最新交易信息
  • 千图网官网免费图广州seo好找工作吗
  • 互联网趋势发展前景北京seo推广服务
  • 做网站都去哪里找模板财经新闻最新消息
  • 网站开发需要的技术人才广州百度竞价开户
  • 宁波网站制作公司十大免费推广平台
  • 网站地址推荐哪个平台可以买卖链接
  • 注册安全工程师管理系统seozou是什么意思
  • 做网站不会P图怎么办填写电话的广告
  • 塔式服务器主机建网站产品推广
  • seo网站推广的主要目的是什么游戏推广员招聘
  • 旅社网站怎么建立北京网站优化seo
  • 找公司做网站多少钱成都郑州百度seo排名公司
  • 徐州企业做网站seo怎么优化网站排名
  • vps网站空间太原做推广营销
  • 密云区建设委员会官方网站网络营销ppt模板
  • 手机网站 源码国内真正的永久免费建站
  • 公司网页需要哪些内容重庆网站seo服务
  • 傻瓜式在线做网站360搜索引擎推广
  • 大连网站建设特色百度浏览器网站入口
  • 做网站用什么字体全面落实疫情防控优化措施
  • 软件下载网站开发 论文站长工具seo综合查询推广
  • 龙华哪有做网站设计网络运营主要做什么工作
  • 邳州做网站seo 优化 工具
  • 谷歌外贸建站多少钱关键词seo报价
  • 怎么在公众号做影视网站搜索引擎营销的案例