In my last post, I had focused on protocol factory class, various methods it needs to provide and also the code flow within which these methods gets called or invoked.
Here we will look into the structure of protocol class , various methods it needs to provide and context in which they are called.
There are two ways to lookup and learn this:
- Look at the interface definition: IProtocol(Interface) in interfaces.py
- Like I did in my previous posting , supply a protocol class with no methods and look at the traceback to understand the code flow
So usual imports for writing a custom protocol:
from twisted.web import proxy
from twisted.internet import reactor
from twisted.internet import protocol
from twisted.python import log
import sys
log.startLogging(sys.stdout)
It is much better to derive from protocol.Protocol to build custom protocol. It does a few things for you.
Any intricate logic should be built using the connect, disconnect, data received event handlers and methods to write data onto connection
makeConnection method sets the transport attribute and also calls connectionMade method . You can use this to start communicating once the connection setup has been established.
dataReceived method is called when there is data to be read off the connection.
connectionLost is called when the transport connection is lost for some reason. To write data on the connection, you use the transport method
self.transport.write. This results in adding the data to buffer which will be sent across the connection. To make twisted send the data or buffer immediately, you can call
self.transport.doWrite