############################################# #socket单线程
Server side:
#!/usr/bin/env python
Import Socket,time,os
Host= "
port=18000
S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
S.bind ((Host,port))
S.listen (1)
While 1:
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 ()
---------------------------------------------------
Client:
#!/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 Multithreading
Server Side
#!/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 ()
-----------------------------------------------------
Client:
#!/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 ()
########################################################################
This article is from the "Galaxy | Computer network" blog, please be sure to keep this source http://qqran.blog.51cto.com/10014850/1961981
The socket for the Python script