Git Product home page Git Product logo

pingpong's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.