Based python socket network programming hello web chat applet

1, the basic concept

1.1socket socket

The socket can be simply understood as a combination of a network address and port, which is based on tcp / ip protocol, a system call to the operating system, as inter-process communication, the communication process is primarily between the different hosts . Because the socket, so generally does not require a network programmer and tcp / ip deal directly with major operating systems generally provide the appropriate API calls. For example, the use of socket, you can bind the appropriate network IP and port number, you can choose to use or to use tcp udp.

1.2tcp and udp

TCP protocol is built on top of the IP protocol. TCP protocol is responsible for establishing a reliable connection between the two computers, in order to ensure that data packets arrive. TCP connection is established through a handshake agreement, then, for each IP packet number, in order to ensure that the other party is received, if the package lost, it automatically re-issued. Many common higher-level protocols are built on the basis of the TCP protocol, such as HTTP protocol for browsers, the sending SMTP protocols.

When using the UDP protocol, without establishing a connection, only need to know each other's IP address and port number, you can send data packets directly. However, we do not know can not reach. Although the use of unreliable UDP transport data, but it has the advantage over and TCP, fast, not requiring reliable data arrives, you can use the UDP protocol.

1.3 Client and Server

When you create a TCP connection, called the client initiates the connection, called passive response server connection. For example, when we visited Sina in your browser, your computer is our client, the browser will initiate connections to the SAN server. If all goes well, Sina accepted our server connection, a TCP connection is established, the communication is to send back the page content.

1.4 Schematic

 

 

2, based on a simple chat program hi the python

2.1 server program

Import Socket 

SK = socket.socket () # default AF_INET, SOCK_STREAM 
address = ( ' 127.0.0.1 ' , 12345 ) 
sk.bind (address) # bind host port number to a socket 
sk.listen (3) # set and start the TCP listener 
Print ( ' waitting ... ' )
 the while True: 
    conn, addr = sk.accept () # passive acceptance of TCP connections, has been waiting for incoming connections 
    Print ( ' incoming connections ' , addr)
     the while True: 
        Data = conn.recv (1024) #Receiving a TCP message, and set the maximum length of the 
        IF  Not Data:
             Print ( ' disconnected! ' ) 
            Conn.Close () 
            BREAK 
        Print (STR (Data, ' UTF8 ' )) 
        InP = INPUT ( '>> ' ) 
        Conn. the send (bytes (InP, ' utf8 ' ))   # end loopback information to customers

Note that using tcp, tcp protocol is represented SOCK_STREAM

2.2 client program

Import   Socket 

SK = socket.socket () # default AF_INET, SOCK_STREAM 
address = ( ' 127.0.0.1 ' , 12345 ) 
sk.connect (address) # connection server 
the while True: 
    InP = INPUT ( '>> ' )
     IF InP == ' Exit ' :
         BREAK 
    sk.send (bytes (InP, ' UTF8 ' )) # sending information to the server 
    Data = sk.recv (1024 )
     Print (STR (Data, ' UTF8 ' ))
else:
    sk.close()

operation result:

 

 

3, use strace to track program code api calls

strace python3 server.py

operation result:

 

 

Guess you like

Origin www.cnblogs.com/ethan123/p/12008818.html