server.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import SocketServer
import os
import threading
class MyHandle(SocketServer.StreamRequestHandler):
def handle(self):
self.data = self.rfile.readline().strip()
print "%s wrote:" % self.client_address[0]
print self.data
self.datas=os.popen(self.data).read()
self.wfile.write(self.datas)
if __name__=='__main__':
addr=""
port=9999
server = SocketServer.ThreadingTCPServer((addr, port), MyHandle)
server_thread = threading.Thread(target=server.serve_forever)
server_thread.setDaemon(True)
server_thread.start()
server.serve_forever()
server.shutdown()
client.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from socket import *
HOST='server ip'
PORT=9999
BUFSIZ = 2048
ADDR = (HOST, PORT)
while True:
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
data = raw_input('> ')
if not data:
break
tcpCliSock.send('%s\r\n' % data)
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
print data.strip()
tcpCliSock.close()
本文转自hahazhu0634 51CTO博客,原文链接:http://blog.51cto.com/5ydycm/368906,如需转载请自行联系原作者