1 # Minimal interface to the Internet telnet protocol.
3 # *** modified to use threads ***
5 # It refuses all telnet options and does not recognize any of the other
6 # telnet commands, but can still be used to connect in line-by-line mode.
7 # It's also useful to play with a number of other services,
8 # like time, finger, smtp and even ftp.
10 # Usage: telnet host [port]
12 # The port may be a service name or a decimal port number;
13 # it defaults to 'telnet'.
22 # Telnet protocol characters
24 IAC
= chr(255) # Interpret as command
32 sys
.stderr
.write('usage: telnet hostname [port]\n')
36 hostaddr
= gethostbyname(host
)
38 sys
.stderr
.write(sys
.argv
[1] + ': bad host name\n')
42 servname
= sys
.argv
[2]
46 if '0' <= servname
[:1] <= '9':
50 port
= getservbyname(servname
, 'tcp')
52 sys
.stderr
.write(servname
+ ': bad tcp service name\n')
55 s
= socket(AF_INET
, SOCK_STREAM
)
58 s
.connect((host
, port
))
60 sys
.stderr
.write('connect failed: %r\n' % (msg
,))
63 thread
.start_new(child
, (s
,))
67 # read socket, write stdout
68 iac
= 0 # Interpret next char as command
69 opt
= '' # Interpret next char as option
71 data
, dummy
= s
.recvfrom(BUFSIZE
)
74 sys
.stderr
.write( '(Closed by remote host)\n')
80 ## print '(replying: %r)' % (opt+c,)
86 cleandata
= cleandata
+ c
88 if c
== DO
: print '(DO)',
91 elif c
in (WILL
, WONT
):
92 if c
== WILL
: print '(WILL)',
96 print '(command)', ord(c
)
101 cleandata
= cleandata
+ c
102 sys
.stdout
.write(cleandata
)
104 ## print 'Out:', repr(cleandata)
107 # read stdin, write socket
109 line
= sys
.stdin
.readline()
110 ## print 'Got:', repr(line)