File: ping.py

package info (click to toggle)
circuits 3.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,980 kB
  • sloc: python: 17,583; javascript: 3,226; makefile: 100
file content (39 lines) | stat: -rwxr-xr-x 1,056 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
"""
Clone of the standard UNIX "ping" command.

This example shows how you can utilize some of the buitlin I/O components
in circuits to write a very simple clone of the standard UNIX "ping" command.
This example merely wraps the standard UNIX "/usr/bin/ping" command in a subprocess
using the ``circuits.io.Process`` Component for Asyncchronous I/O communications with
the process.
"""

import sys

from circuits import Component, Debugger
from circuits.io import Process, stdout, write


class Ping(Component):
    # This adds the already instantiated stdout instnace
    stdout = stdout

    def init(self, host):
        self.p = Process(['/bin/ping', host]).register(self)
        self.p.start()

    def read(self, data):
        """
        read Event Handler

        This is fired by the File Component when there is data to be read
        from the underlying file that was opened.
        """
        self.fire(write(data), stdout)


# Start and "run" the system.
app = Ping(sys.argv[1])
Debugger().register(app)
app.run()