python36

1. Socket development history and classification

Sockets originated in the 1970s at the University of California, Berkeley version of Unix, or BSD Unix. Therefore, sometimes people also refer to sockets as "Berkeley sockets" or "BSD sockets". In the beginning, sockets were designed to communicate between multiple applications on the same host. This is also known as inter-process communication, or IPC. There are two kinds of sockets (or called two races), which are file-based and network-based.

Socket family:

The name of the file type socket family: AF_UNIX (unix is ​​all files, file-based sockets call the underlying file system to fetch data, two socket processes run on the same machine, can access the same The file system completes the communication indirectly)

The name of the network-type socket family: AF_INET (and AF_INET6 are used for ipv6, and there are some other address families, but they are either only used for a certain platform, or they have been abandoned, or very few It is used or not implemented at all. Of all the address families, AF_INET is the most widely used one. Python supports many address families, but because we only care about network programming, most of the time we only use AF_INET)

The earliest socket was not used for network communication, but to deal with the communication between two processes on the same machine.
     -The original two processes do not belong to the same memory space, and the memory spaces are isolated from each other. How to solve this problem?
    -Solution: The data between the hard disks is shared with each other. One process writes the data to the file, and the other process reads the file to achieve the purpose of data communication.

 Second, the socket workflow

 

 

import socket 

# 1 、 Buy mobile 
phone phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # Streaming protocol = 》 tcp protocol 

# 2 、 Bound mobile phone card 
phone.bind (( ' 127.0.0.1 ' , 8083 )) # 0 - 65535 , 1024 have been previously retained by the system 

# 3 , start 
phone.listen ( . 5 ). 5 # refers to the size of the connection pool semi 
Print ( ' server startup is complete, the listener address:% s:% s ' % ( ' 127.0.0.1 ' , 8080 )) 

# 4 , waiting for the phone connection request: get the phone connection conn 
conn, client_addr =phone.accept () 

# 5 、 Communication: receive \ send message
 while True:
     try : 
        data = conn.recv ( 1024 ) # The maximum amount of data received is 1024Bytes , what is received is bytes type
         if len (data) == 0 : 
            # Wash in the unix system, once the data received is empty 
            # means an abnormal behavior: the client illegally disconnected the link 
            break 
        print ( " Message from the client: " , data.decode ( ' utf -8 ' )) 
        conn.send (data.upper ()) 
    except Exception: 
        # for windows system 
        break 

# 6 , close the phone connection conn (required operation of recycling resources) 
conn.close ()

# 7 , shut down (optional operation) 
phone.close ()
import socket 

# 1 、 Buy a mobile 
phone phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # Streaming protocol = 

tcpprotocol 
# 2 、 Call the server phone.connect (( ' 127.0.0.1 ' , 8083 ) ) 

# 3 、 Communication
 while True: 
    msg = input ( " Enter the message to be sent >>>: " ) .strip () # msg = '' 
    if len (msg) == 0 : continue 
    phone.send (msg.encode ( ' utf-8 ' )) 
    print ( ' ======? ' )
    data = phone.recv ( 1024 ) 
    print (data.decode ( ' utf-8 ' )) 

# 4 , close the connection (required operation of recycling resources) 
phone.close ()

 

Guess you like

Origin www.cnblogs.com/zhangjiahao996/p/12741592.html