python- network programming, simple model

  

  Client

# This is the client 
. 1
Import Socket 2 . 3 # holding the phone . 4 Client = socket.socket () . 5 . 6 # dial server ip and port write . 7 the client.connect (( ' 127.0.0.1 ' , 8080 )) . 8 . 9 10 client.send (B ' I AM Client ' ) # of people talk . 11 12 is Data = client.recv (1024) # listening to others 13 # most read 1024, (the memory read) 14 15 16 Print (Data) . 17client.close () # on the phone

  

 Server

This is the server
 Import socket 


# is similar to buy a mobile phone to call the 
Server socket.socket = ()     # class is instantiated 
# _ (Self, Family = AF_INET, of the type = SOCK_STREAM, proto = 0, fileno = None): 
      # Inter mode (AF_INE default: TCP 

# inserted telephone card 
server.bind (( ' 127.0.0.1 ' , 8080))    # the bind placed inside a tuple (host, port), bind ip and port 

# boot 
server.listen (. 5)     # set semijoin pool 
  # set connection pool is half wait for a connection set up to the client how many, because it can only communicate with a client
Print ( ' 123 ' ) # wait for someone to call (blocked) conn, addr = server.accept () #Monitor (blocking) # listening to others, to accept the 1024 data the Data = conn.recv (1024) # (obstruction) # most read 1024, (reading the memory) # others answer conn.send (b ' the Hello ' ) # conn.send ( ''. encode ( 'UTF-. 8')) Print (Data) # hang conn.Close () # shutdown server.close () Print ( ' over ' )

 

 

 

Guess you like

Origin www.cnblogs.com/pscly/p/11347955.html