建立服务器与客户端的持续连接
iServer
#coding:utf-8
from socket import *
from os import *
s = socket(AF_INET,SOCK_STREAM)
s.bind(('',6666))
s.listen(1)
while True:
sock,addr = s.accept()
print 'connected by ',addr
while True: #持续连接,直到客户端退出
cmd = sock.recv(1024)
if cmd == 'exit': #退出关闭连接
sock.close()
break
result = popen(cmd).read()
sock.send(result)
iClient
#coding:utf-8
from socket import *
c = socket(AF_INET,SOCK_STREAM)
c.connect(('127.0.0.1',6666))
while True:
cmd = raw_input('cmd ? ')
c.send(cmd)
if cmd == 'exit' :
c.close()
break
data = c.recv(1024)
print data
持续连接建立成功
缺陷:
无法切换目录执行命令