python | socket | ssh ---- ====== server ====== import commands,socket class MySocketServer: HOST='www.tnkyou.com' PORT=50000 RECEIVE_SIZE=8192 def __init__(self): self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM) self.s.bind((self.HOST, self.PORT)) self.s.listen(4) def run(self): self.conn,self.addr = self.s.accept() print 'from: '+str(self.addr) while True: data = self.conn.recv(self.RECEIVE_SIZE) data = data.strip() if data == 'exit': self.conn.sendall("bye") self.close() break status,result = commands.getstatusoutput(data) if len(result.strip()) != 0: print status,result print self.conn.sendall(result) else: self.conn.sendall(" ") def close(self): self.conn.close() self.s.close() def __del__(self): self.close() if __name__ == '__main__': server = MySocketServer() server.run() server='' ---- ====== Client ====== import commands,socket class MySocketClient: # HOST='localhost' # HOST='www.tnkyou.com' HOST='121.40.142.12' PORT=50000 RECEIVE_SIZE=8192 def __init__(self): self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM) self.s.connect((self.HOST, self.PORT)) def run(self): while True: cmd = raw_input(">") cmd = cmd.strip() if len(cmd.strip()) == 0:continue self.s.sendall(cmd) data = self.s.recv(self.RECEIVE_SIZE) if data == 'bye': print "bye" self.close() break print data def close(self): self.s.close() def __del__(self): self.close() if __name__ == '__main__': client = MySocketClient() client.run() client = ''