Software testing engineers must know technology: Python takes you to get started with WebSocket

About Socket those things

Socket, that is, network socket, is the endpoint (abstract) of a two-way communication channel.

Sockets can communicate within a process, between processes on the same machine, or between processes on different machines.

Network sockets can be implemented through many different channel types, such as TCP, UDP, etc.

In other words, a network socket is an endpoint in a communication flow between two running programs in a computer network. This endpoint is a virtual abstraction that does not exist in any hardware.

A network socket can be uniquely identified by a combination of IP address and port number. Below we briefly introduce the terms related to Socket sockets, so as to facilitate the understanding of the follow-up content.

Domain

As a transport mechanism in a network, a domain is a member of a protocol. The values ​​of these fields are constants, such as AF_INET, PF_INET, PF_UNIX, PF_X25, etc. These constants belong to the address family and are used to specify the address type that the socket can communicate with.

Type

Indicates the type of communication between two endpoints, usually SOCK_STREAM and SOCK_DGRAM.

SOCK_STREAM

Based on TCP, data transmission is relatively guaranteed, and it is a guaranteed connection-oriented Socket, which can ensure that data is transmitted to the other party correctly; transmission based on data stream; network programming using TCP/IP protocol.

SOCK_DGRAM

Based on UDP, used for undirected connection protocol, it is an unsecured message-oriented socket, mainly used for broadcasting information on the network; transmission based on data packets; network programming using UDP protocol.

Protocol

Used to identify protocol variants within domains and types. Its default value is 0 and is usually omitted.

Hostname

The identifier of the network interface. The hostname can be a string, a dotted quad address, or an IPV6 address in colon (and possibly dot) notation.

Port

Each server listens on a port for calls to one or more clients.

PythonSocket module

To implement Socket programming in Python , you need to use the Socket module. We can create a Socket with the following syntax:

# Import the socket library and create a simple socket

import socket

s = socket.socket (socket_family, socket_type, protocol = 0)

When creating a Socket, different parameters can be used (see above for related concepts):

·socket_family:AF_UNIX or AF_INET

·socket_type:SOCK_STREAM或SOCK_DGRAM

protocol: this is usually ignored and defaults to 0

SocketMethods socket method

Python provides three different methods for sockets, namely "server socket method", "client socket method", and "general socket method".

(1) Server socket method

In the client-server architecture (C/S architecture), there is a central server to provide services, and many different clients receive services from the central server. Many different clients also make requests to this central server.

Some important server socket methods in the C/S architecture are:

socket.bind()

Bind a server address (hostname, port number) to a socket.

socket.listen()

Listening for connections with the socket, this method starts the TCP listener; the parameters in this method are used to specify the maximum number of queued connections (the minimum value is 0, and the maximum value is 5).

socket.accept()

Accept TCP client connection, this method will return a pair of values ​​(conn, address), where conn is a new socket object for sending and receiving data; address is the address bound to the socket. Before using this method, you must first use the socket.bind() and socket.listen() methods.

(2) Client socket method

In a client-server architecture (C/S architecture), the client requests the server and also receives services from the server. socket.connect(address) This method actively establishes a server connection. In short, the function of this method is to connect the client to the server.

(3) Generic socket method

In addition to the client and server socket methods, there are some common socket methods that are also very useful in Socket socket programming.

Commonly used generic socket methods are:

·.recv(bufsize)

Used to receive TCP messages from sockets;

The parameter bufsize represents the buffer size, which defines the maximum amount of data that the method can receive at any time.

·socket.send(bytes)

Used to send data to the socket connected to the remote machine; the parameter bytes indicates the bytes sent to the socket

number.

·socket.recvfrom(data,address)

Used to receive data from the socket;

This method returns two pairs of values ​​(data, address); among them, data indicates the received data, and address indicates the socket address of the sending data.

·socket.sendto(data,address) is used to send data to the socket;

This method returns two pairs of values ​​(data, address); where data represents the number of bytes sent and address represents the address of the remote machine.

· socket.close () is used to close the socket.

Socket.gethostname() returns the name of the host.

socket. sendall(data)

Send all data to the socket connected to the remote machine until an error occurs;

During this period, if an error occurs, the socket is closed using the socket.close() method.

4. Application: establish a connection between the server and the client through Python

In order to establish a connection between the server and the client, we need to write two programs in Python, one for the server and one for the client.

【Server program】

(1) socket.bind() method

In the server-side Socket socket program, use the socket.bind() method to bind it to the specified IP address and port number, so that the incoming request on the IP and port can be monitored .

(2) socket.listen () method

Then, use the socket.listen() method to set the server into listening mode. This method has a parameter, which means that when the server is busy, how many connections are currently allowed to keep waiting; for example, socket.listen(2), it means that 2 connections are currently allowed to keep waiting, if the third socket tries to connect, then The connection request will be rejected.

(3) socket. send() method

Use the socket.send() method to send a message to the client.

(4) socket.accept method

Use the socket.accept() method to initiate the connection.

(5) socket.close() method

Use the socket.close() method to close the connection.

……

[A full 200 episodes] A collection of super-detailed advanced tutorials for automated testing of Python interfaces, a real simulation of actual enterprise project combat

Guess you like

Origin blog.csdn.net/dq565/article/details/132549386