##############################################socket單線程python
server端:多線程
#!/usr/bin/env pythondom
import socket,time,ossocket
host=''ide
port=18000線程
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)server
s.bind((host,port))ip
s.listen(1)utf-8
while 1:input
conn,addr=s.accept()
print 'connected by',addr
while 1:
data=conn.recv(8192)
cmd=os.popen(data)
cmd_smg='\033[32;1mFeedback of the cmd\033[0m\n'+cmd.read()
if not data:break
#conn.sendall(data.upper())
conn.sendall(cmd_smg)
print 'connected by',addr
#print 'receievd cmd:'%data.upper()
print cmd_smg
conn.close()
---------------------------------------------------
客戶端:
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
import socket,time,random,tab
host='192.168.1.10'
port=18000
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
while 1:
#smg=random.randint(1,100)
cmd=raw_input('input cmd:')
#s.sendall('hello my name is xiaohong:%s'%smg)
s.sendall(cmd)
data=s.recv(8192)
print "Received from server:",data
time.sleep(1.2)
s.close()
###################################################socket server多線程
server端
#!/usr/bin/env python
import SocketServer
class MyTCPHandle(SocketServer.BaseRequestHandler):
def handle(self):
while 1:
self.data=self.request.recv(8192)
if not self.data:
print 'connected break'
break
print 'Connected from:',self.client_address
print self.data
self.request.sendall(self.data.upper())
host,port='',9999
server=SocketServer.ThreadingTCPServer((host,port),MyTCPHandle)
server.serve_forever()
-----------------------------------------------------
客戶端:
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
import socket,time,random,tab
host='192.168.1.10'
port=9999
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
while 1:
#smg=random.randint(1,100)
cmd=raw_input('input cmd:')
cmd=cmd.strip()
if len(cmd)==0:continue
#s.sendall('hello my name is xiaohong:%s'%smg)
s.sendall(cmd)
data=s.recv(8192)
print "Received from server:",data
#time.sleep(1.2)
s.close()
########################################################################