服务器端:
import SocketServer
import os
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
print self.client_address
while 1:
self.data=self.request.recv(4096).strip()
if not self.data:continue
cmd = os.popen(self.data)
jieguo=cmd.read()
print self.data,jieguo,'\n'
self.request.sendall(jieguo)
host,port='',9999
server=SocketServer.ThreadingTCPServer((host,port),MyTCPHandler)
server.serve_forever()
客户端:
#!/usr/local/python
import tab
import socket
from time import sleep
host='192.168.2.2'
port=9999
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
while 1:
input=raw_input('inpu:')
s.send(input)
date=s.recv(4096)
print 'REeceived',date
s.close()