我正在使用python tcp shell;我希望能够telnet到一个端口,让它用shell提示我:ex。
$ telnet localhost 5555
Connected to localhost.
Escape character is '^]'.
$
下面的答案非常有助于让我继续前行。这是工作代码。
import SocketServer, os, subprocess
class EchoRequestHandler(SocketServer.BaseRequestHandler ):
def setup(self):
print self.client_address, 'connected!'
self.request.send(str(self.client_address) + '\n')
self.request.send(os.system('sh'))
def handle(self):
data = 'dummy'
while data:
data = self.request.recv(1024)
self.request.send(os.popen(data.strip()).read())
if data.strip() == 'bye':
return
def finish(self):
print self.client_address, 'disconnected!'
self.request.send('bye ' + str(self.client_address) + '\n')
server = SocketServer.ThreadingTCPServer(('', 7784), EchoRequestHandler)
server.serve_forever()
答案 0 :(得分:6)
import os
os.system('sh') # or bash, tcsh, zsh, whatever. :-P