如何在Python中使用tulipasyncio创建中继服务器?

如何在Python中使用tulipasyncio创建中继服务器?,第1张

概述我有 example echo server import asyncioclass EchoServer(asyncio.Protocol): def connection_made(self, transport): peername = transport.get_extra_info('peername') print('connection f 我有 example echo server

import asyncioclass EchoServer(asyncio.Protocol):    def connection_made(self,transport):        peername = transport.get_extra_info('peername')        print('connection from {}'.format(peername))        self.transport = transport    def data_received(self,data):        self.transport.write(data)        # ClIEnt pIEce goes hereloop = asyncio.get_event_loop()coro = loop.create_server(EchoServer,'127.0.0.1',8888)server = loop.run_until_complete(coro)print('serving on {}'.format(server.sockets[0].getsockname()))try:    loop.run_forever()except KeyboardInterrupt:    print("exit")finally:    server.close()    loop.close()

我正在尝试做的是添加一个客户端部分,我评论说它将连接到一台新服务器并从那个方向发送数据.这是echo client,但我需要一个看起来像这样的流程:

+-----------+    +-----------+    +--------------+    | My Server |    | My ClIEnt |    | Other Server |    +-----------+    +-----------+    +--------------+          |                |                 | ===>Get some data         |                 |          |                |                 |      Send data ---------->|                 |          |                |                 |          |            Send data ----------->|          |                |                 |          |                |              Do Stuff          |                |                 |          |                | <-----------Send Data          |                |                 |          | <--------- Send data             |          |                |                 | <=== Send data            |                 |          |                |                 |          |                |                 |          |                |                 |          |                |                 |

显然,我可以同步执行此 *** 作,但我正在尝试创建客户端 – >其他服务器位异步,我真的不知道如何使用asyncio方法在我的服务器块和客户端之间进行通信.

我需要做什么?

解决方法 这是一个简单的代理,允许你wget 127.0.0.1:8888并从谷歌得到一个HTML回复:

import asyncioclass ClIEnt(asyncio.Protocol):    def connection_made(self,transport):        self.connected = True        # save the transport        self.transport = transport    def data_received(self,data):        # forward data to the server        self.server_transport.write(data)    def connection_lost(self,*args):        self.connected = Falseclass Server(asyncio.Protocol):    clIEnts = {}    def connection_made(self,transport):        # save the transport        self.transport = transport    @asyncio.coroutine    def send_data(self,data):        # get a clIEnt by its peername        peername = self.transport.get_extra_info('peername')        clIEnt = self.clIEnts.get(peername)        # create a clIEnt if peername is not kNown or the clIEnt disconnect        if clIEnt is None or not clIEnt.connected:            protocol,clIEnt = yIEld from loop.create_connection(                ClIEnt,'Google.com',80)            clIEnt.server_transport = self.transport            self.clIEnts[peername] = clIEnt        # forward data to the clIEnt        clIEnt.transport.write(data)    def data_received(self,data):        # use a task so this is executed async        asyncio.Task(self.send_data(data))@asyncio.coroutinedef initialize(loop):    # use a coroutine to use yIEld from and get the async result of    # create_server    server = yIEld from loop.create_server(Server,8888)loop = asyncio.get_event_loop()# main task to initialize everythingasyncio.Task(initialize(loop))# runloop.run_forever()
总结

以上是内存溢出为你收集整理的如何在Python中使用tulip / asyncio创建中继服务器?全部内容,希望文章能够帮你解决如何在Python中使用tulip / asyncio创建中继服务器?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1194112.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存