[Python interface automation]--In-depth understanding of the basic composition of the HTTP interface and the principle of web page construction

Table of contents

introduction

1. Introduction to HTTP

2. HTTP principle and web page foundation

     2.1. Basic principles of HTTP

     2.2, HTTP request process

    2.3. Web page composition


introduction

Python interface automation has a wide range of application scenarios, but in actual use, some problems may arise. For example, you don't know the basic composition of the HTTP interface, and you don't know how web pages are constructed.

At this time, you need to have a deep understanding of the basic composition of the HTTP interface and the principles of web page construction. Through the study of this article, you will quickly master key skills that will make your work easier and more enjoyable.

1. Introduction to HTTP

        The HTTP protocol is the abbreviation of Hyper Text Transfer Protocol (Hypertext Transfer Protocol), which is a transmission protocol used to transmit hypertext from a World Wide Web (WWW: World Wide Web) server to a local browser. HTTP is a communication protocol based on TCP/IP to transfer data (HTML files, image files, query results, etc.). You can check Baidu Encyclopedia's introduction to the HTTP protocol in detail. A simple understanding is that HTTP is an object-oriented stateless protocol belonging to the application layer. The HTTP protocol works on the client-server architecture. The browser acts as an HTTP client through The URL sends all requests to the HTTP server, that is, the WEB server. The web server returns response information to the client according to the received request.

2. HTTP principle and web page foundation

     2.1. Basic principles of HTTP

       When we visit a web site, we first enter a URL in the browser, and then we can access the website. What are URLs? The full name of URL is: Universal Resource Locator, which is a uniform resource locator, a concise representation of the location and access method of resources that can be obtained from the Internet, and is the address of standard resources on the Internet. It includes the access protocol http (https), access path and resource name, port number, etc., such as https://www.baidu.com.

The full name of HTTP: Hyper Text Transfer Protocol, translated into Chinese as Hypertext Transfer Protocol. The HTTP protocol is used to transmit hypertext data from the network to a local browser, and to ensure efficient and accurate transmission of hypertext documents, and the HTML source code is a hypertext markup language. The protocol was jointly formulated by the World Wide Web and the Internet Working Group, and the currently widely used version is HTTP1.1. In addition to HTTP, there is also HTTPS, which is the secure version of HTTP. The content transmitted through HTTPS is encrypted by SSL to ensure the security of data transmission. For example, the website we use to buy tickets: https://www.12306.cn/ is such a site.

     2.2, HTTP request process

        We enter a URL site in the browser, and after pressing Enter, we can see the content of the website page in the browser normally. In fact, this process is that the browser sends a request to the server where it is located, telling the server what to request, and the server processes and parses the request after receiving the request. If the processing and parsing of the request is OK, it returns to the corresponding browser. the response to. The response includes the source code of the page and other content, and the browser then parses the source code in the response, and finally presents the parsed web page. The general process is: browser request "server processing and analysis" server response and pass it to the browser "browser parses and renders the web page.

Next, through the actual network site, learn the process of HTTP request and response, and what kind of network requests occur in the process. Take Chrome browser to visit Baidu site: https://www.baidu.com/ as an example.

 Open the Chrome browser, right-click the menu and select [Inspect] or the direct shortcut key [F12] to open the developer tools and switch to [Network]. Enter the Baidu site in the browser: https://www.baidu.com/, press Enter, and see the network requests that occur. As shown below:

 

 The first network request: www.baidu.com is a request sent by the browser to the Baidu server to access the Baidu site. Click on this request to see the detailed information and content of the request.

The first is the General section, which describes the URL of the request, the method of the request, the response status code, and the address and port of the remote server;

Next, there are Response Headers and Request Headers, which are response headers and request headers, respectively. It contains some relevant information and content of the response and request. Let's take a look at the content of this request and the corresponding response.

composition of the request

The request is sent from the client, namely the browser, to the server, and the sent request has four aspects: request method (Request Method), requested site (Request URL), request header (Request Headers), request body (Request Body)

1. Request method

Common requests are get, post, put... types. The get type interface generally refers to the interface for obtaining information, such as the function of list query. Clicking the query button calls a get interface, and then returns the information. It means pulling the content from the server (backend). The Post type is generally the function of submitting a form, such as registration, importing data, etc. are post interfaces. It means pushing the content to the server (backend).

2. Requested site

The requested website is a URL entered in the browser

3. Request header

The request header is used to describe the attachment information used by the server, that is, the additional content stipulated by the HTTP protocol, which must be handled in accordance with the protocol rules. Access the request header information of Baidu site as shown in the figure below:

 

 Look at the relevant field descriptions of the request header information

Accept: Request header field, used to specify what types of information the client can accept, such as text/html and other types of information can be accepted in the above figure

Accept-Encoding and Accept-Language: Specify the encoding and language acceptable to the client

Connection: connection status

Cookie: Cookie information stored, mainly used to maintain the current session

Host: the address of the site that needs to be accessed

User-Agent: Used to identify information such as the operating system and browser version used by the customer to the server

4. Request body

The request body generally carries the form data of the POST type request, which is usually encapsulated in the request body and sent using json format data, and the GET type request body is generally empty.

Composition of the response

In response to the information returned by the server to the browser, the content of the response has three aspects: the response status code (Response Status Code), the response header (Response Headers) and the response body (Response Body)

1. Response status code

The response status code indicates the processing result of the server for the request information, such as 200 means the response is normal, 404 means the page is not found, 500 means the server error, etc.

 2. Response header

The response header contains the response information of the server to the request, as shown in the following figure:

 

Look at the description of the relevant key fields in the response header

Content-Type: document type, specify what the returned document is, such as text/html means that the returned document is an HTML document

Server: server information, such as server name, version, etc.

Set-Cookie: Set Cookie

Expires: Specify the expiration time of the response

3. Response body

Generally, it is the information that needs to be returned to the client after processing the HTTP interface request in the client mode. For example, most of the response boby is the data information encapsulated into JSON format and returned to the client.

2.3. Web page composition

       Modern webpages are always colorful and rich in visual experience. Different webpages often have a variety of appearances, with reasonable layout, rich pictures, animation effects, etc. So how are these pages made up? There are three main parts that make up a webpage: HTML, CSS, and JavaScript. Among them, HTML constitutes the basic structure of the webpage, CSS determines the layout style of the webpage, and JavaScript determines the plasticity and dynamic presentation of the webpage. Let's look at these three parts in detail:

1.HTML

HTML: The full name is Hyper Text Markup Language, that is, Hypertext Markup Language. Elements such as text, paragraphs, pictures, and buttons on a web page are defined by HTML, such as img tags for pictures, p tags for paragraphs, etc. Open the Baidu site in the Chrome browser, right-click the mouse menu and select [Check] or directly [ F12] Open the developer tool and you can see the HTML source code of the webpage in the [Elements] option bar, as shown below:

 

You can see the HTML source code of the webpage. Each tag pair defines a node and the attributes of the node, and they form an HTML tree. These node label pairs are displayed in the HTML tree, and they have a certain hierarchical relationship, which is often represented by parent nodes, child nodes, and sibling nodes. Specifically for learning HTML, you can refer to the W3School website: http://www.w3school.com.cn/html/index.asp

2.CSS

CSS: The full name is Cascading Style Sheets, which is Cascading Style Sheets. CSS is a standard used to determine the typesetting style of a web page, specifying attributes such as the size, color, and position of text in a web page. Locate the [Baidu click] button as shown in the figure below to view the style:

 

 

 

The CSS style determines the width and height of the button, that is, the pixel size of the width and height, and the text color color: white and other information, the button background color: background

3.JavaScript

JavaScript is JS, a scripting language used to embed JS files into HTML codes to provide interactive dynamic effects, such as prompt boxes, carousels, download progress bars, and so on. Its tag pairs in HTML are defined by script tag pairs

To sum up, HTML defines the content and structure of web pages, CSS describes the layout rendering and position effects of web page elements, and JavaScript defines the interactivity and animation effects of web pages. These three constitute the infrastructure for rich web page presentation.

After learning about the HTTP request interface and the basic composition of web pages, and having a clear understanding of the interface of the HTTP protocol, we will conduct automated testing of the interface based on HTTP


-Learn a little every day, and you will become a master in the future-

[Message: Software testing and learning] Get the link to the network disk below


-There must be a way, and then there will be success- Finally, I wish everyone to reach the ceiling of the test as soon as possible!

If you don’t want to grow wildly alone, can’t find system information, can’t get help for problems, and give up after persisting for a few days, you can join our Q skirt: 321255410 , everyone can discuss and exchange together, there will be various Various software testing materials and technical exchanges.

Today I mainly share my experience and network disk learning resources with you. I will continue to share some relevant test materials in the future. Friends who have been helped, you can like and support~

 

Guess you like

Origin blog.csdn.net/weixin_67553250/article/details/131046395