如何使用twisted手动发送数据 [英] How to send data manually using twisted

查看:36
本文介绍了如何使用twisted手动发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是扭曲框架的新手.

而且我知道有很多回调函数会自动触发

And I know there are many callback function will trigger automatically

连接建立或断开时.

但我不知道如何在没有这些回调的情况下发送数据.

But I have no idea how to send the data without those callbacks.

例如,我想放置一个方法 custom_write() 用于将数据发送出去.

For example , I want to put an method custom_write() for sending the data out.

    def custom_write(self,data):
        self.transport.write(
            data)

并在我的 main(): 方法中触发该函数.

And trigger the function in my main(): method.

def main():
    try:
        p_red("I'm Client")
        f = EchoFactory()
        reactor.connectTCP("localhost",
                            8000,
                            f)

reactor.custom_write("HAHAHA")

如果我在不同的端口创建多个反应器绑定怎么办.

And what if I create multiple reactor binding in different port.

例如:localhost:1234,localhsot:5678

并将不同的两条消息发送到 2 个连接.

and send the different two messages to the 2 connections.

例如:"Thanks" to port 1234 , Bye~ to port 5678

任何信息都可以给我.

谢谢

class EchoClient(protocol.Protocol):

    def connectionMade(self):
        self.transport.write(
            "I'm cli")

    def custom_write(self,data):
        self.transport.write(
            data)

    def dataReceived(self, data):
        print "Server said:", data 
        self.transport.loseConnection()
        pass

    def connectionLost(self, reason):
        print("[{0}] Lose connection...".format(
            self.__class__.__name__)
        )
        pass        

class EchoFactory(protocol.ClientFactory):

    protocol = EchoClient
    """docstring for EchoFactory"""
    def clientConnectionFailed(self,
                               connector,
                               reason):
        print "[{0}] Connection failed - goodbye".format(
            self.__class__.__name__)
        reactor.stop()

    def clientConnectionLost(self,
                             connector,
                             reason):        
        print "[{0}] Connection lost - goodbye".format(
            self.__class__.__name__)    
        reactor.stop()

def main():
    try:
        p_red("I'm Client")
        f = EchoFactory()
        reactor.connectTCP("localhost",
                            8000,
                            f)
        try:
            reactor.run()
        except BaseException  as e:
            traceback.print_exc(file=sys.stdout)
            raise e

        pass
    except BaseException  as e:
        traceback.print_exc(file=sys.stdout)
        raise e

    pass

推荐答案

您可以多次调用 connectTCP() 并使用不同的主机、端口.connectTCP() 立即返回,无需等待完整交换完成.要发送不同的字符串,您可以将它们传递到可以使它们可用于协议的工厂.

You can call connectTCP() more than once and using different hosts, ports. connectTCP() returns immediately without waiting for the full exchange to complete. To send different strings, you could pass them into the factories that can make them available to protocols.

这篇关于如何使用twisted手动发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆