服务器端:
#!/usr/bin/env python
#conding=utf-8
import SocketServer
import commands
import time
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
while True:
self.data = self.request.recv(1024).strip()
if not self.data:
break
print self.data
user_input = self.data.split()
if user_input[0] == 'get':
with open(user_input[1],'rb') as f:
self.request.sendall(f.read())
time.sleep(0.5)
self.request.send('FileTransferDone')
continue
cmd_status,result = commands.getstatusoutput(self.data)
if len(result.strip()) != 0:
self.request.sendall(result)
else:
self.request.sendall("Done")
if __name__ == "__main__":
host,port = "localhost",9991
server = SocketServer.ThreadingTCPServer((host,port), MyTCPHandler)
server.serve_forever()