English 中文(简体)
扭曲和脚本中的更多客户端
原标题:twisted and more clients in script

Hello
I m new in twisted, but have some questions after read manual:
1. How to use different protocols with different reactor in one program? (for example, txNetTools have own reactor and internal IRC support in twisted have own reactor from twisted.internet)
2. How to start more one client in one time? (many client with ping to other remote host) http://bazaar.launchpad.net/~oubiwann/txnet/trunk/view/head:/sandbox/ping.py
3. How to put data from one protocol to other (in one program)? I m want use data from database in protocol. (for example, every 5 minutes get hosts from database and create ping clients)
My task is simple, create a more different protocol client to many count of servers.

最佳回答

好吧,至少对于第三个问题,你说的是使用不同类的协议还是同一类的多个协议实例?协议实例可以通过让创建它们的工厂存储它们的数据来在彼此之间进行通信,如下所示:

class p(Protocol):
    factory = None
    ...
class f(Factory):
    protocol = p
    data = None
    def buildProtocol(self, addr):
        returnValue = p()
        returnValue.factory = self
        return returnValue

从那里你可以将数据保存到self.factory.data中的协议实例,任何其他协议实例都可以访问它。我希望这能回答你的问题。

问题回答

如何在一个程序中使用不同的协议和不同的反应器?

你没有。每个进程只有一个反应器,它可以处理你想要的任意多个连接。绝大多数库都不提供反应器,txNetTools提供的反应器是可选的。它唯一提供的是这种方法:

def listenICMP(self, port, protocol, interface="", maxPacketSize=8192):
    p = icmp.Port(port, protocol, interface, maxPacketSize, self)
    p.startListening()
    return p

如果您想使用另一个reactor,那么您可以自己实例化一个icmp.Port

如何一次启动多个客户端?

和你开始的一样,只是重复了一遍。例如,这里有十个并发的ping(包含第一个问题的答案):

for i in range(10):
    p = icmp.Port(0, Pinger(), reactor=reactor)
    p.startListening()
reactor.run()

查梅科很好地回答了最后一个问题。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签