2011-04-27 5 views
1

twisted.protocols.telnet의 Telnet이 더 이상 사용되지 않습니다. 그러나 twisted.conch.telnet의 최신 텔넷이 어떻게 대체되는지는 알 수 없습니다. 그것은 잘 작동하지만 새로운 Telnet 클래스는 이러한 표준 방법이 전혀 없습니다HowTo에 의한 텔넷 twisted.conch.telnet?

from twisted.protocols.telnet import Telnet 

class MyProtocol(Telnet): 

    def welcomeMessage(self): 
     return "Hi, Welcome to my telnet server" 

    def loginPrompt(self): 
     return "Who are you ?" 

    def checkUserAndPass (self,u,p): 
     # some stuff here 

    def telnet_Command(self, cmd): 
     self.transport.write("you typed " + cmd) 

: 나는이 이렇게 가고, 텔넷 인증 코드의 매우 간단한 조각을 썼다. AuthenticatingTelnetProtocol도 체크 아웃했는데, 대부분은 문서화되어 있지 않습니다. 누구든지 위의 코드와 동일하거나 대체로 재 작성하는 예제로 나를 가리킬 수 있습니까? 미리 감사는

답변

1

twisted.protocols.telnettwisted.conch.telnet 사이의 가장 큰 차이점은 텔넷 프로토콜 (RFC 854)과의 이전 구현 부분 후자 반면 표준 사용자 이름/암호 로그인 스타일 세션에 적용 일부 "편리 성"기능을 추가 텔넷 프로토콜을 모두 구현하고 "편리한"기능을 응용 프로그램 개발자에게 제공합니다.

다행히도 "편리함"기능은 구현하기 어렵지 않습니다. 이것은 기본적으로 두 가지입니다. 클라이언트의 라인을 파싱하고 연결이 어떤 스테이지 (또는 "상태")인지에 따라 다른 메소드를 호출합니다. LineReceiver은 전자를 수행하며 후자는 간단합니다. 따라서, 예를 들어 :

from twisted.protocols.basic import LineReceiver 
from twisted.conch.telnet import TelnetProtocol 

class SimpleTelnetSession(LineReceiver, TelnetProtocol): 

    def connectionMade(self): 
     self.transport.write('Username: ') 
     self.state = 'USERNAME' 

    def lineReceived(self, line): 
     getattr(self, 'telnet_' + self.state)(line) 

    def telnet_USERNAME(self, line): 
     self.username = line 
     self.transport.write('Password: ') 
     self.state = 'PASSWORD' 

    ... 

이 너무 많거나 적은 무엇 AuthenticatingTelnetProtocol를 구현이다.