Based on a complete Python hello / hi simple network chat program

A, Socket socket Introduction

Socket (Socket) is an abstraction layer, the application can send or receive data through it, it can be the same as file open, close, and write operations. Sockets allow an application I / O into the network, and communicate with other applications in the network. Network socket is a combination of IP addresses and ports.

To-end communications transport layer, therefore, each of the transport layer connection has two ends. So, what transport layer connection endpoint is it? IP addresses are not the host, not the host, not the application process, nor is the transport layer protocol port. Transport layer connection endpoint called socket (socket). According to the definition of RFC793: port number to the IP address spliced ​​constitute a socket. The so-called socket, in fact, is a communication endpoint, each socket has a socket number, IP address of the host and comprises a 16-bit host port number, i.e. the form (host IP address: port number) . For example, if the IP address is 210.37.145.1, and port number is 23, then the socket is obtained (210.37.145.1:23).

Second, the Python implementation based on a simple network chat program

This example uses the Socket API interface function program in Python is: The client sends a message to the server, the server returns a message to the client, in order to achieve a simple chat network.

server.py

Import Socket 
S = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 
s.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, . 1 ) 
s.bind (( ' 127.0.0.1 ' , 9999))   # listening port 
s.listen (1)   # call to listen () method starts listening port, the incoming parameter specifies the maximum number of connections waiting for 
our sock, addr = s.accept () 
buf = sock.recv (1024 ) .decode () 

the while True:
     IF   buf! = ' Exit ' :
         Print ( ' client:' buf +) 
        Data = INPUT ( ' server: ')
        sock.send(data.encode())
        if  data=='exit':
        break

     buf =sock.recv(1024).decode()

 client.py

Import Socket
 Import SYS
 # Create a Socket 
S = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
 # establish a connection 
s.connect (( ' 127.0.0.1 ' , 9999 ))
 the while True:
     # transmitting data: 
    the try : 
        Data = the INPUT ( " the client said: " ) 
        s.send (data.encode ()) 
        buf = s.recv (1024 ) .decode ()
         IF ! buf = ' Exit ' :
             Print ( " the server said: " + buf)
    except:
        print("Dialogue Over")
        s.close()
        sys.exit(0)

 Open two command-line window, run a server program, another program running the client, you can see the effect.

 

 

 

 

 

Third, the comparison of Python socket API and Linux socket API

Python Linux
socket (socket.AF_INET, socket.SOCK_STREAM)   socket(PF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_IP) = 3
connect((host,port)) connect(3,{sa_family=AF_INET,sin_port=htons(12345),sin_addr("127.0.0.1")},[16]))=4
serv_sock.accept() accept(3,{sa_family=AF_INET,sin_port=htons(12345),sin_addr("127.0.0.1")},[16]))=4
serv_sock.bind((host,port)) bind(3,{sa_family=AF_INET,sin_port=htons(12345),sin_addr("0.0.0.0")},16)=0
accept() accept(3,{sa_family=AF_INET,sin_port=htons(2345),sin_addr("192.168.8.xx)},[16]))=4

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/xiehuichina/p/12024421.html