Socket network module of python

1. Socket

1 Introduction

       The original meaning of socket in English is "hole" or "socket". As the process communication mechanism of BSD UNIX, the latter means. Usually
called "socket", it is used to describe the IP address and port. It is the handle of a communication chain and can be used to realize the communication between different virtual machines or different computers. Hosts on the Internet generally run multiple service software and provide several services at the same time. Each service opens a Socket and binds it to a port, and different ports correspond to different services. Socket is just like its English original meaning, like a multi-hole socket. A host is like a room full of sockets. Each socket has a number. Some sockets provide 220V AC, some provide 110V AC, and some provide cable TV programs. The client software plugs the plugs into the sockets with different numbers to get different services.

2. Connection principle

       According to the way the connection is started and the destination of the local socket connection, the connection process between the sockets can be divided into three steps: server monitoring, client request, and connection confirmation.

(1) Server monitoring: The server-side socket does not locate the specific client socket, but is in a state of waiting for connection, real-time monitoring of the network status.

(2) Client request: refers to a connection request made by the client's socket, and the target to be connected is the server's socket. For this reason, the socket of the client must first describe the socket of the server it wants to connect to, point out the address and port number of the server-side socket, and then make a connection request to the server-side socket.

(3) Connection confirmation: When the server-side socket monitors or receives the connection request of the client-side socket, it will respond to the request of the client-side socket and establish a new thread to transfer the server-side socket. The description of the socket is sent to the client. Once the client confirms the description, the connection is established. The server socket continues to be in the listening state and continues to receive connection requests from other client sockets.

2. The use of socket

       In Python, we use the socket function of the socket module to create a socket object. The syntax format is as follows

socket.socket ( family ,type ,proto)

parameter

  • family: The socket family can be AF_UNIX or AF_INET
  • type: socket type, can be based on connection-oriented SOCK_STREAM or non-connection SOCK_DGRAM
  • protocol: Generally do not fill in, the default is 0

 

Server (server.py)

import socket
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)  #创建socket对象,面向连接的
server.bind(("127.0.0.1",9999))    #绑定主机名和端口号
server.listen(10)                #设置最大连接数,超过后排队
clientsock,addr=server.accept()  #建立与客户端的连接,返回(socket object, address info)元组对象
print("addr=%s %s"%(addr,type(addr)))  
print("客户端ip地址为:%s  端口号为:%s"%addr)
clientsock.send("欢迎来到服务端,你打球的时候好像蔡徐坤".encode('utf-8'))  #python3要求发送byte型的数据,所以我们将它以utf-8的形式转换为bytes类型的
msg=clientsock.recv(1024)    #接收客户端发来的消息,msg2为bytes类型的数据
print(msg.decode('utf-8'))   #我们将bytes类型数据转换成str字符型的数据,以utf-8的形式
clientsock.close()

Client (client.py)

import socket
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(("127.0.0.1",9999))  #connect函数接收元组型数据
msg=client.recv(1024)         #接收从服务器发来的消息,为bytes类型的数据,大小为1024字节的缓冲区
print(msg.decode('utf-8'))    #我们转化为str字符串类型的数据,以utf-8的形式
client.send("客户端到此一游,who are you ?".encode('utf-8'))  #python3要求发送bytes类型的数据,所以我们得将它转换
client.close()

Execute server.py first, then execute the client

 

 

 

Guess you like

Origin blog.csdn.net/qq_44159028/article/details/114841935