Telnet Client ExampleΒΆ

telnet.py

 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python

import os
import optparse
from socket import gethostname
 
from circuits.io import stdin
from circuits import handler, Component
from circuits import __version__ as systemVersion
from circuits.net.sockets import TCPClient, UNIXClient, Connect, Write

USAGE = "%prog [options] host [port]"
VERSION = "%prog v" + systemVersion

def parse_options():
    parser = optparse.OptionParser(usage=USAGE, version=VERSION)
   
    opts, args = parser.parse_args()
   
    if len(args) < 1:
        parser.print_help()
        raise SystemExit, 1
   
    return opts, args
   
class Telnet(Component):
   
    channel = "telnet"

    def __init__(self, *args):
        super(Telnet, self).__init__()
   
        if len(args) == 1:
            if os.path.exists(args[0]):
                self += UNIXClient(channel=self.channel)
                host = dest = port = args[0]
                dest = (dest,)
            else:
                raise OSError("Path %s not found" % args[0])
        else:
            self += TCPClient(channel=self.channel)
            host, port = args
            port = int(port)
            dest = host, port
   
        print "Trying %s ..." % host
        self.push(Connect(*dest), "connect")
   
    def connected(self, host, port=None):
        print "Connected to %s" % host
   
    def error(self, *args):
        if len(args) == 3:
            type, value, traceback = args
        else:
            value = args[0]
            type = type(value)
            traceback = None
   
        print "ERROR: %s" % value
   
    def read(self, data):
        print data.strip()
   
    @handler("read", target="stdin")
    def stdin_read(self, data):
        self.push(Write(data), "write")
   
opts, args = parse_options()
(Telnet(*args) + stdin).run()

Previous topic

Echo Server Example

Next topic

IRC Bot Example

This Page