English 中文(简体)
如何将议定书的两个不同阶段分开处理?
原标题:How to separate two different phases of a protocol in Twisted?

我试图学会如何与Twisted做 st,而我只想一 concept一:: 2. 订立一项议定书,基本上分两个阶段进行交流: 首先是短手和认证,然后是实际工作。

我的冷静态度是起草一份这样的议定书:

def stringReceived(self, data):
    if self.state == "authenticate":
        handle_auth(data)
    else:
        handle_actual_work(data)

我花了很强的时间,用扭曲的方式做这项工作。 以上情况是否正常? 在我看来,起草一份认证的议定书,另一个议定书仅涉及经认证的客户,但我究竟是怎样做?

我审视了类似的问题。 Twisted:我如何确定初步联系议定书,然后下放到适当的议定书执行?。 这里的解决办法与我目前的做法基本相同。 这种做法是否真的正确?

最佳回答

是的,你的版本相当正常。 值得称道的是,它应当更多地支持国家机器,因为它在内部对不同议定书、不同事件组合等的一般模式有大约100个不同的实施。 但是,这一部分实际上并没有打上手势,它本身是:Twisted正在向网络提出反对,并将要求它采取方法(在这种情况下,stringReceived)。 你与这一信息做些什么完全取决于您的标语和? 声明是完全合理的。

At this level, the question is not really about the "twisted way" but, rather, about better Python idioms for state machines and methods which are context-dependent. Depending on the exact needs of your protocol, your current approach may be fine, or you might want to dispatch to a method with a special name, like this:

def stringReceived(self, data):
    getattr(self, "stringReceived_{}".format(self.state))(data)

def stringReceived_authenticate(self, data):
    if self.auth_ok(data):
       self.state =  normal 
    else:
       self.transport.loseConnection()

def stringReceived_normal(self, data):
    self.do_stuff(data)

... or getting even fancier, you might want to use decorators, or metaclasses, or something else entirely. Nothing is wrong with any of these approaches.

问题回答

暂无回答




相关问题
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 ]="...