[英]Application of Twisted Python in TCP/IP connections?
I am writing a client that needs to establish several independent communication channels, each with its own unique port on the server, with a server through a series of sending and receiving messages. 我正在编写一个客户端,该客户端需要建立几个独立的通信通道,每个通道在服务器上具有自己的唯一端口,并通过一系列发送和接收消息与服务器建立连接。 I know how to do this using socket send and recv, by giving each communication channel a socket, and do send and recv on that socket.
我知道如何使用套接字发送和接收来实现此目的,方法是为每个通信通道分配一个套接字,并在该套接字上进行发送和接收。 I need to make this work in Twisted, and found potentially useful interfaces including Factory and ProcessProtocol.
我需要在Twisted中进行这项工作,并找到了可能有用的接口,包括Factory和ProcessProtocol。 However, the Protocol interfaces do not provide a method to send messages.
但是,协议接口不提供发送消息的方法。 Is ProcessProtocol a good choice for my task, and how I make ProcessProtocol send messages?
ProcessProtocol是执行任务的理想选择吗?如何使ProcessProtocol发送消息?
In case you don't know about it, I'd like to give a shout out to the excellent Twisted finger
tutorial that goes through the library at a good pace but with enough detail that you know what's going on. 如果您不了解它,我想向优秀的Twisted
finger
教程大喊大叫,该教程以不错的速度通过了库,但是提供了足够的详细信息,您知道正在发生的事情。
To directly answer your question, though, I'd say you're on the right track with Protocol and (Client)Factory. 但是,要直接回答您的问题,我想说您在Protocol和(Client)Factory方面是正确的。 I think the cleanest way to do what you're looking for (assuming you need to connect to different ports because they're outputs for different data) would be to make a factory/protocol pair for each port you want to connect to/handle, and then use an external class to handle the application logic aggregating all of them.
我认为做您要寻找的东西的最干净的方法(假设您需要连接到不同的端口,因为它们是不同数据的输出)将为您要连接/处理的每个端口建立一个工厂/协议对,然后使用外部类来处理将所有逻辑汇总在一起的应用程序逻辑。 Generally you wouldn't want your application logic mixed deeply with your networking logic.
通常,您不希望您的应用程序逻辑与网络逻辑深入融合。
A simple example: (note the use of self.transport.write to send data) 一个简单的例子:(注意使用self.transport.write发送数据)
from twisted.internet.protocol import Protocol, ClientFactory
from sys import stdout
from foobar_application import CustomAppObject
class FooProtocol(Protocol):
def connectionMade(self):
# Use self.transport.write to send data to the server
self.transport.write('Hello server this is the Foo protocol.')
self.factory.do_app_logic()
class FooFactory(ClientFactory):
protocol = FooProtocol
def __init__(self, app_object=None):
self.app = app_object
def do_app_logic(self):
self.app.do_something()
class BarProtocol(Protocol):
def dataReceived(self, data):
stdout.write('Received data from server using the Bar protocol.')
self.factory.do_fancy_logic(data)
class BarFactory(ClientFactory):
protocol = BarProtocol
def __init__(self, app_object=None):
self.app = app_object
def do_fancy_logic(self, data):
self.app.do_something_else(data)
logic_obj = CustomAppObject()
reactor.listenTCP(8888, FooFactory(app_object=logic_obj)
reactor.listenTCP(9999, BarFactory(app_object=logic_obj)
reactor.run()
You might also want to look at the 'Writing Clients' docs on the Twisted site . 您可能还需要查看Twisted网站上的“ Writing Clients”文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.