Package twisted :: Package protocols :: Module telnet :: Class Telnet
[frames | no frames]

Class Telnet

BaseProtocol --+    
               |    
        Protocol --+
                   |
                  Telnet

Known Subclasses:
Shell

I am a Protocol for handling Telnet connections. I have two sets of special methods, telnet_* and iac_*.

telnet_* methods get called on every line sent to me. The method to call is decided by the current mode. The initial mode is 'User'; this means that telnet_User is the first telnet_* method to be called. All telnet_* methods should return a string which specifies the mode to go into next; thus dictating which telnet_* method to call next. For example, the default telnet_User method returns 'Password' to go into Password mode, and the default telnet_Password method returns 'Command' to go into Command mode.

The iac_* methods are less-used; they are called when an IAC telnet byte is received. You can define iac_DO, iac_DONT, iac_WILL, iac_WONT, and iac_IP methods to do what you want when one of these bytes is received.
Method Summary
  connectionMade(self)
I will write a welcomeMessage and loginPrompt to the client.
  dataReceived(self, data)
Called whenever data is received.
  iac_DO(self, feature)
  iac_DONT(self, feature)
  iac_IP(self, feature)
  iac_WILL(self, feature)
  iac_WONT(self, feature)
  iacSBchunk(self, chunk)
  loggedIn(self)
Called after the user succesfully logged in.
  loginPrompt(self)
Override me to return a 'login:'-type prompt.
  processChunk(self, chunk)
I take a chunk of data and delegate out to telnet_* methods by way of processLine.
  processLine(self, line)
I call a method that looks like 'telnet_*' where '*' is filled in by the current mode.
  telnet_Command(self, cmd)
The default 'command processing' mode.
  telnet_Password(self, paswd)
I accept a password as an argument, and check it with the checkUserAndPass method.
  telnet_User(self, user)
I take a username, set it to the 'self.username' attribute, print out a password prompt, and switch to 'Password' mode.
  welcomeMessage(self)
Override me to return a string which will be sent to the client before login.
  write(self, data)
Send the given data over my transport.
    Inherited from Protocol
  connectionFailed(self)
(Deprecated)
  connectionLost(self, reason)
Called when the connection is shut down.
    Inherited from BaseProtocol
  makeConnection(self, transport)
Make a connection to a transport and a server.

Class Variable Summary
str buffer = ''
list delimiters = ['\r\n', '\r\x00']
int echo = 0                                                                     
int gotIAC = 0                                                                     
NoneType iacByte = None                                                                  
NoneType lastLine = None                                                                  
str mode = 'User'
    Inherited from Protocol
tuple __implements__ = (<class twisted.internet.interfaces.IPr...
    Inherited from BaseProtocol
int connected = 0                                                                     
NoneType transport = None                                                                  

Method Details

connectionMade(self)

I will write a welcomeMessage and loginPrompt to the client.
Overrides:
twisted.internet.protocol.BaseProtocol.connectionMade

dataReceived(self, data)

Called whenever data is received.

Use this method to translate to a higher-level message. Usually, some callback will be made upon the receipt of each complete protocol message.
Parameters:
data - a string of indeterminate length. Please keep in mind that you will probably need to buffer some data, as partial (or multiple) protocol messages may be received! I recommend that unit tests for protocols call through to this method with differing chunk sizes, down to one byte at a time.
Overrides:
twisted.internet.protocol.Protocol.dataReceived (inherited documentation)

loggedIn(self)

Called after the user succesfully logged in.

Override in subclasses.

loginPrompt(self)

Override me to return a 'login:'-type prompt.

processChunk(self, chunk)

I take a chunk of data and delegate out to telnet_* methods by way of processLine. If the current mode is 'Done', I'll close the connection.

processLine(self, line)

I call a method that looks like 'telnet_*' where '*' is filled in by the current mode. telnet_* methods should return a string which will become the new mode. If None is returned, the mode will not change.

telnet_Command(self, cmd)

The default 'command processing' mode. You probably want to override me.

telnet_Password(self, paswd)

I accept a password as an argument, and check it with the checkUserAndPass method. If the login is successful, I call loggedIn().

telnet_User(self, user)

I take a username, set it to the 'self.username' attribute, print out a password prompt, and switch to 'Password' mode. If you want to do something else when the username is received (ie, create a new user if the user doesn't exist), override me.

welcomeMessage(self)

Override me to return a string which will be sent to the client before login.

write(self, data)

Send the given data over my transport.

Class Variable Details

buffer

Type:
str
Value:
''                                                                     

delimiters

Type:
list
Value:
['\r\n', '\r\x00']                                                     

echo

Type:
int
Value:
0                                                                     

gotIAC

Type:
int
Value:
0                                                                     

iacByte

Type:
NoneType
Value:
None                                                                  

lastLine

Type:
NoneType
Value:
None                                                                  

mode

Type:
str
Value:
'User'                                                                 

Generated by Epydoc 2.0 on Sat May 15 20:07:58 2004 http://epydoc.sf.net