与扭曲发送任意数据 [英] Sending arbitrary data with Twisted

查看:192
本文介绍了与扭曲发送任意数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code的例子如下。我想在程序的不同点到arbitrarly发送数据。扭曲似乎很大听,然后反应,但如何我只是发送数据。

 从twisted.internet.protocol进口DatagramProtocol
    从twisted.internet进口反应堆
    进口OS    类监听器(DatagramProtocol):        高清__init __(个体经营):        高清datagramReceived(个体经营,数据,(主机,端口)):
            打印得到+数据        高清send_stuff(数据):
            self.transport.write(数据,(主机,端口))    reactor.listenUDP(10000,监听())
    reactor.run()    ##有些事情发生在程序独立的连接状态的
    ##现在怎么我访问send_stuff


解决方案

您已经例如包括一些code发送数据:

 高清send_stuff(数据):
        self.transport.write(数据,(主机,端口))

在换句话说,回答你的问题是电话send_stuff甚至叫transport.write。

在你问评论:

  #Now如何我访问send_stuff

有什么特别的是如何当你使用双绞线您的访问对象或方法。这是一样的,你可以写任何其他Python程序。使用变量,属性,容器,函数参数,或任何其他设施,以保持的引用的对象。

下面是一些例子:

 #保存监听器实例中的局部变量
网络监听=()
reactor.listenUDP(10000,网络)#使用局部变量的GUI事件连接到网络
MyGUIApplication()。connect_button(send_button的network.send_stuff)#使用局部变量来实现发送数据信号处理程序
高清report_signal(*忽略):
    reactor.callFromThread(network.send_stuff,得到SIGINT)
signal.signal(signal.SIGINT,report_signal)#路过的局部变量引用的对象到另一个初始化
#与网络相关的对象,因此它可以保存其上的参考和以后调用方法
#当它到达事件就是了回应。
reactor.listenUDP(20000,AnotherDatagramProtocol(网络))

等。

An example of my code is as follows. I would like to arbitrarly send data at various points in the program. Twisted seems great for listening and then reacting, but how to I simply send data.

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor
    import os

    class listener(DatagramProtocol):

        def __init__(self):

        def datagramReceived(self, data, (host, port)):
            print "GOT " + data

        def send_stuff(data):
            self.transport.write(data, (host, port))

    reactor.listenUDP(10000, listener())
    reactor.run()

    ##Some things happen in the program independent of the connection state
    ##Now how to I access send_stuff

解决方案

Your example already includes some code that sends data:

    def send_stuff(data):
        self.transport.write(data, (host, port))

In other words, the answer to your question is "call send_stuff" or even "call transport.write".

In a comment you asked:

#Now how to I access send_stuff

There's nothing special about how you "access" objects or methods when you're using Twisted. It's the same as in any other Python program you might write. Use variables, attributes, containers, function arguments, or any of the other facilities to maintaining references to objects.

Here are some examples:

# Save the listener instance in a local variable
network = listener()
reactor.listenUDP(10000, network)

# Use the local variable to connect a GUI event to the network
MyGUIApplication().connect_button("send_button", network.send_stuff)

# Use the local variable to implement a signal handler that sends data
def report_signal(*ignored):
    reactor.callFromThread(network.send_stuff, "got sigint")
signal.signal(signal.SIGINT, report_signal)

# Pass the object referenced by the local variable to the initializer of another
# network-related object so it can save the reference and later call methods on it
# when it gets events it wants to respond to.
reactor.listenUDP(20000, AnotherDatagramProtocol(network))

And so on.

这篇关于与扭曲发送任意数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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