Skip to content

Ping Pong is a library for quickly creating line-based interactive programs.

License

Notifications You must be signed in to change notification settings

dgvncsz0f/pingpong

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ping Pong

Ping Pong is a library for quickly creating line-based interactive programs.

Motivation

This has been created to provide a mechanism to test programs using expect. We are using it to create phony interfaces that although do nothing, provide the same interface as the real device. This allows us to test these applications easily.

Besides, special care has been taken to be able to use different protocols. Currently there is a TTY and SSH backends. The later makes use of twisted, so it should be fairly simple to add other protocols.

Dependencies

Main library

Refer to setup.py

Testing

  • nose
  • mock

Example

Examples are available in src/conductor/samples. Following, there is an example of a session that responds to ping commands:

# -*- coding: utf-8 -*-

from pingpong.engine import term
from pingpong.engine import ssh
from pingpong import session

class ping_session(session.interactive_session):

    def on_begin(self):
        self._prompt()

    def on_end(self):
        pass

    def on_abort(self):
        self._endl()
        self._prompt()

    def on_line(self, line):
        """
          This is the key. =interactive_session= invokes this method
          everytime the peer sends a newline.
        """
        if (line == "ping"):
            self.transport.write("pong")
        self._endl()
        self._prompt()

    def _prompt(self):
        self.transport.write("$ ")

    def _endl(self):
        self.transport.write("\r\n")

if (__name__ == "__main__"):
    e = term.tty_engine()  # Uses the current terminal as th engine.
    
    # e = ssh.ssh_engine() # Anternatively you may use SSH protocol as
                           # well. To test it, you may use:
                           #   $ ssh -o UserKnownHostsFile=/dev/null -p 2222 root@localhost
                           # When asked to enter the password, use `password'.
                           #
                           # N.B.: The -o flag is used because we
                           #       create a new pair of RSA keys
                           #       everytime.
    e.run(ping_session)

Testing

$ make tests

Changelog

  • Version 0.0.1 <2011-09-13 Tue 18:04>

    First version

About

Ping Pong is a library for quickly creating line-based interactive programs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages