Advertisement
Guest User

Untitled

a guest
May 19th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from twisted.internet import stdio, reactor, protocol, defer
  4. from twisted.protocols import basic
  5.  
  6. class RoombaConsole(basic.LineReceiver):
  7. delimiter = '\n' # unix terminal style newlines. remove this line
  8. # for use with Telnet
  9.  
  10. def connectionMade(self):
  11. self.sendLine("Roomba Remote Management console. Type 'help' for help.")
  12.  
  13. self.factory = RFactory()
  14. self.connector = reactor.connectTCP('localhost', 8000, self.factory)
  15.  
  16. def lineReceived(self, line):
  17. # Ignore blank lines
  18. if not line: return
  19. self.issueCommand(line)
  20.  
  21. def issueCommand(self, line):
  22. # Parse the command
  23. commandParts = line.split()
  24. command = commandParts[0].lower()
  25. args = commandParts[1:]
  26.  
  27. # Dispatch the command to the appropriate method. Note that all you
  28. # need to do to implement a new command is add another do_* method.
  29. try:
  30. method = getattr(self, 'do_' + command)
  31. except AttributeError, e:
  32. self.sendLine('Error: no such command.')
  33. else:
  34. try:
  35. method(*args)
  36. except Exception, e:
  37. self.sendLine('Error: ' + str(e))
  38.  
  39. self.factory.deferred.addCallback(self._checkResponse)
  40.  
  41. def do_help(self, command=None):
  42. """help [command]: List commands, or show help on the given command"""
  43. if command:
  44. self.sendLine(getattr(self, 'do_' + command).__doc__)
  45. else:
  46. commands = [cmd[3:] for cmd in dir(self) if cmd.startswith('do_')]
  47. self.sendLine("Valid commands: " +" ".join(commands))
  48.  
  49. def do_quit(self):
  50. self.sendLine('Goodbye.')
  51. self.transport.loseConnection()
  52.  
  53. def do_check(self, url):
  54. self.connector.transport.write("Peffff\n")
  55.  
  56. def _checkResponse(self, args):
  57. success, line = args
  58. self.sendLine(line)
  59. self.factory.deferred = defer.Deferred()
  60.  
  61.  
  62. class Client(basic.LineReceiver):
  63.  
  64. def connectionMade(self):
  65. self.cmd_success=True
  66. return
  67.  
  68. def lineReceived(self, line):
  69. self.factory.deferred.callback((self.cmd_success, line))
  70.  
  71. def connectionLost(self, reason):
  72. reactor.stop()
  73.  
  74. class RFactory(protocol.ClientFactory):
  75. protocol = Client
  76.  
  77. def __init__(self):
  78. self.deferred = defer.Deferred()
  79.  
  80.  
  81. if __name__ == "__main__":
  82. stdio.StandardIO(RoombaConsole())
  83. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement