<div dir="ltr"><br><br>I want to create a server in twisted that has a permanent variable which i want to use as the number of request requested to the server. When i use that for Protocol, each time the variable is created newly.<br>
<br>Then i used<br><b>class Echo(Protocol):<br>&nbsp;&nbsp;&nbsp; def dataReceived(self, data):<br>&nbsp;&nbsp;  <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if &quot;data&quot; not in dir(self.factory):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.factory.data = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.factory.data = self.factory.data + 1<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;As soon as any data is received, write it back.&quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.transport.write(data + str(self.factory.data))<br><br><br>but i want to remove the code that is:<br><br></b><i><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if &quot;data&quot; not in dir(self.factory):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.factory.data = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.factory.data = self.factory.data + 1<br><br><br>How to do that?<br></b></i></div>