Server:
 
 
 
 # Server </P> <p> Import socket </P> <p> address = ('2017. 0.0.1 ', 31500) <br/> S = socket. socket (socket. af_inet, socket. sock_stream) # S = socket. socket () <br/> S. BIND (Address) <br/> S. listen (5) </P> <p> SS, ADDR = S. accept () <br/> Print 'got connected from', ADDR </P> <p> SS. send ('byebye ') <br/> Ra = ss. recv (512) <br/> Print Ra </P> <p> SS. close () <br/> S. close () <br/>
 
 
 
 
 
 
 
Client:
 
 
# Client </P> <p> Import socket </P> <p> address = ('2017. 0.0.1 ', 31500) <br/> S = socket. socket (socket. af_inet, socket. sock_stream) <br/> S. connect (Address) </P> <p> DATA = s. recv (512) <br/> Print 'the data has ed', data </P> <p> S. send ('hihi') </P> <p> S. close () <br/>
 
 
 
 
 
 
 
Running result:
 
 
 
 
 
 
 
Server
 
 
 
[Work@db-testing-com06-vm3.db01.baidu.com Python] $ Python server. py
Got connected from ('2017. 0.0.1 ', 127)
Hihi
 
 
 
 
 
 
 
 
 
 
 
Client
 
 
 
[Work@db-testing-com06-vm3.db01.baidu.com Python] $ Python client. py
The data already ed is Byebye
 
 
 
 
 
 
========================================================== ==============================================
 
 
 
 
 
 
 
 References:Http://www.cppblog.com/lai3d/archive/2008/02/19/42919.html
 
 
 
 
 
 
 
A simple example of server-Client Communication
 
 
 
Server:
 
 
 
 
 
   Import   Socket
S   =   Socket. socket ()
S. BIND ((   '   XXX. XXX   '  , Xxxx ))   #   IP address and port number   
   S. Listen (   5   )
CS, address   =   S. Accept ()
   Print       '   Got connected from   '   , Address
CS. Send (   '  Byebye   '   )
RA   =   CS. Recv (   512   )
   Print   RA
CS. Close ()  
 
 
 
 
 
 
 
 
 
Client:
 
 
 
 
 
   Import   Socket
S   =  Socket. socket ()
S. Connect ((   '   XXX. XXX   '   , Xxxx ))   #   And ServerProgramThe IP address and port number are the same.   
   Data   =   S. Recv (   512   )
S. Send (   '   Hihi   '  )
S. Close ()
   Print       '   The data already ed is   '   , Data  
 
 
 
 
 
 
 
 
 
Run:
 
 
 
Test on the local machine (in Windows, you can change the IP address to the IP address of the local machine, the port number is above 1024, and Windows will retain the IP address below 1024), run -- cmd -- to enter the command line mode
 
 
 
First, the python server program, and then the python client program.
 
 
 
You can also use the Telnet IP address and port number after starting the server program.
 
    
 
Allow the server to continuously accept connections
 
 
 
Server. py
 
 
 
 
 
  Import   Socket
S   =   Socket. socket ()
S. BIND ((   '   192.168.43.small   '   ,   2000   ))
S. Listen (   5  )
 
 
 While     1  :
CS, address =  S. Accept ()
  Print     '  Got connected from  '  , Address
CS. Send (  '  Hello I am server, welcome  '  )
RA  =  CS. Recv (  512 )
  Print  RA
CS. Close () 
 
 
 
 
 
 
 
Test whether two sockets exist in one program.
 
 
Client. py
 
  Import  Socket
S  =  Socket. socket ()
S. Connect ((  '  192.168.43.small  '  , 2000  ))
Data  =  S. Recv (  512  )
  Print     '  The data already ed is/n  '  , Data
S. Send (  '  Hihi I am Client  ' )
 
 
Sock2=  Socket. socket ()
Sock2.connect ((  '  192.168.43.small  '  ,  2000  ))
Data2  =  Sock2.recv (  512  )
  Print     '  The data already ed from server is/n '  , Data2
Sock2.send (  '  Client send use sock2  '  )
Sock2.close () 
 
S. Close ()