这只是一个简单的c/s架构,理想化得获取客户端输入的指令,并且将输出打印到客户端界面,大家可以试着玩一下。嘿嘿。
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()
如果想了解更多,请关注我们的公众号
公众号ID:opdevos
扫码关注