Telnet Tutorial¶
Overview¶
Welcome to our 2nd circuits tutorial. This tutorial is going to walk you through the telnet Example showing you how to various parts of the circuits component library for building a simple TCP client that also accepts user input.
Be sure you have circuits installed before you start:
pip install circuits
See: Installing
Components¶
You will need the following components:
All these are available in the circuits library so there is nothing for you to do. Click on each to read more about them.
Design¶
strict digraph { TCPClient -> Select [weight="2.0"]; Telnet -> TCPClient [weight="1.0"]; Telnet -> File [weight="1.0"]; }The above graph is the overall design of our Telnet application. What’s shown here is a relationship of how the components fit together and the overall flow of events.
For example:
Connect to remote TCP Server.
Read input from User.
Write input from User to connected Socket.
Wait for data from connected Socket and display.
Implementation¶
Without further delay here’s the code:
1#!/usr/bin/env python
2
3import sys
4
5from circuits import Component, handler
6from circuits.io import File
7from circuits.net.events import connect, write
8from circuits.net.sockets import TCPClient
9
10
11class Telnet(Component):
12
13 channel = "telnet"
14
15 def init(self, host, port):
16 self.host = host
17 self.port = port
18
19 TCPClient(channel=self.channel).register(self)
20 File(sys.stdin, channel="stdin").register(self)
21
22 def ready(self, socket):
23 self.fire(connect(self.host, self.port))
24
25 def read(self, data):
26 print(data.strip())
27
28 @handler("read", channel="stdin")
29 def read_user_input(self, data):
30 self.fire(write(data))
31
32
33host = sys.argv[1]
34port = int(sys.argv[2])
35
36Telnet(host, port).run()
Discussion¶
Some important things to note…
Notice that we defined a
channel
for outTelnet
Component?This is so that the events of
TCPClient
andFile
don’t collide. Both of these components share a very similar interface in terms of the events they listen to.
class Telnet(Component):
channel = "telnet"
Notice as well that in defining a
channel
for ourTelnet
Component we’ve also “registered” theTCPClient
Component so that it has the same channel as ourTelnet
Component.Why? We want our
Telnet
Component to receive all of the events of theTCPClient
Component.
TCPClient(channel=self.channel).register(self)
In addition to our
TCPClient
Component being registered with the samechannel
as ourTelnet
Component we can also see that we have registered aFile
Component however we have chosen a different channel here calledstdin
.Why? We don’t want the events from
TCPClient
and subsequently ourTelnet
Component to collide with the events fromFile
.So we setup a Component for reading user input by using the
File
Component and attaching an event handler to ourTelnet
Component but listening to events from ourstdin
channel.
File(sys.stdin, channel="stdin").register(self)
@handler("read", channel="stdin")
def read_user_input(self, data):
self.fire(write(data))
Here is what the event flow would look like if
you were to register the Debugger
to the Telnet
Component.
from circuits import Debugger
(Telnet(host, port) + Debugger()).run()
$ python telnet.py 10.0.0.2 9000
<registered[telnet] (<TCPClient/telnet 21995:MainThread (queued=0) [S]>, <Telnet/telnet 21995:MainThread (queued=4) [R]> )>
<registered[stdin] (<File/stdin 21995:MainThread (queued=0) [S]>, <Telnet/telnet 21995:MainThread (queued=5) [R]> )>
<registered[*] (<Debugger/* 21995:MainThread (queued=0) [S]>, <Telnet/telnet 21995:MainThread (queued=5) [R]> )>
<started[telnet] (<Telnet/telnet 21995:MainThread (queued=4) [R]> )>
<registered[select] (<Select/select 21995:MainThread (queued=0) [S]>, <TCPClient/telnet 21995:MainThread (queued=0) [S]> )>
<ready[telnet] (<TCPClient/telnet 21995:MainThread (queued=0) [S]> )>
<ready[stdin] (<File/stdin 21995:MainThread (queued=0) [S]> )>
<connect[telnet] ('10.0.0.2', 9000 )>
<_open[stdin] ( )>
<connected[telnet] ('10.0.0.2', 9000 )>
<opened[stdin] ('<stdin>', 'r' )>
Hello World!
<_read[stdin] (<open file '<stdin>', mode 'r' at 0x7f32ff5ab0c0> )>
<read[stdin] ('Hello World!\n' )>
<write[telnet] ('Hello World!\n' )>
<_write[telnet] (<socket._socketobject object at 0x11f7f30> )>
<_read[telnet] (<socket._socketobject object at 0x11f7f30> )>
<read[telnet] ('Hello World!\n' )>
Hello World!
^C<signal[telnet] (2, <frame object at 0x12b0a10> )>
<stopped[telnet] (<Telnet/telnet 21995:MainThread (queued=0) [S]> )>
<close[telnet] ( )>
<close[stdin] ( )>
<disconnected[telnet] ( )>
<closed[stdin] ( )>
Testing¶
To try this example out, download a copy of the
echoserver Example
and copy and paste the full source code of the
Telnet
example above into a file called telnet.py
.
In one terminal run:
$ python echoserver.py
In a second terminal run:
$ python telnet.py localhost 9000
Have fun!
For more examples see examples.