= 用telnet操作一个twisted服务器(Using telnet to manipulate a twisted server) = 在开始之前,我们要建立一个只允许远程访问Python解释器的服务器。我们将用telnet来访问这个服务器 To start things off, we're going to create a simple server that just gives you remote access to a Python interpreter. We will use a telnet client to access this server. ---- 在命令行运行mktap telnet -p 4040 -u admin -w admin。如果你现在查看你的当前目录,你会发现多了一个新文件:telnet.tap。然后你再运行twistd -f telnet.tap。这个telnet服务器将会侦听所有连接到你所指定的4040端口的连接。试着用你喜欢的telnet工具连接到127.0.0.1的404端口。 Run mktap telnet -p 4040 -u admin -w admin at your shell prompt. If you list the contents of your current directory, you'll notice a new file -- telnet.tap. After you do this, run twistd -f telnet.tap. Since the Application has a telnet server that you specified to be on port 4040, it will start listening for connections on this port. Try connecting with your favorite telnet utility to 127.0.0.1 port 4040. {{{ $ telnet localhost 4040 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. twisted.manhole.telnet.ShellFactory Twisted 1.1.0 username: admin password: admin >>> }}} 现在,你应该可以看到Python的提示符:>>>。你可以在这里输入任何合法的Python代码,我们来试试吧: Now, you should see a Python prompt -- >>>. You can type any valid Python code here. Let's try looking around. {{{ #!python >>> dir() ['__builtins__'] }}} 很好,再来一些复杂点的: Ok, not much. let's play a little more: {{{ #!python >>> import __main__ >>> dir(__main__) ['__builtins__', '__doc__', '__name__', 'os', 'run', 'string', 'sys'] >>> service >>> service._port >>> service.parent }}} service对象是用来响应telnet连接的服务器,它正在用一个叫ShellFactory的东西来侦听4040端口。它的父类是一个服务的集合,叫做twisted.application.service.MultiService。我们可以一直得到服务父类的属性,直到我们到达这个tap中所有服务的根。 The service object is the service used to serve the telnet shell, and that it is listening on port 4040 with something called a ShellFactory. Its parent is a twisted.application.service.MultiService, a collection of services. We can keep getting the parent attribute of services until we hit the root of all services in this tap. ---- 正如你所看到的,这个真的很有用,我们可以观察一个正在运行的进程,可以看它内部的对象,甚至可以改变这些对象的属性。我们可以往已有的tap添加telnet支持,如:mktap --append=foo.tap telnet -p 4040 -u user -w pass。这个telnet服务器当然也可以由python代码直接使用。你可以通过看twisted.tap.telnet的代码来了解具体怎么做。 As you can see, this is quite useful - we can introspect a running process, see the internal objects, and even change their attributes. We can add telnet support to existing tap like so: mktap --append=foo.tap telnet -p 4040 -u user -w pass. The telnet server can of coursed be used from straight Python code as well. You can see how to do this by reading the code for twisted.tap.telnet. ---- 最后,如果你想要更安全的访问,你可以让telnet服务器使用SSL。如果你有了适当的证书和私钥文件,你可以运行mktap telnet -p ssl:443:privateKey=mykey.pem:certKey=cert.pem -u admin -w admin。想到了解更多有关侦听端口的参数请参考twisted.application.strports。 A final note - if you want access to be more secure, you can even have the telnet server use SSL. Assuming you have the appropriate certificate and private key files, you can mktap telnet -p ssl:443:privateKey=mykey.pem:certKey=cert.pem -u admin -w admin. See twisted.application.strports for more examples of options for listening on a port.