HTTP protocol ~ Two

Source: Internet
Author: User
Tags http authentication http post ssl certificate

HTTP protocol (2)

HTTP (hypertext Transfer Protocol) is a set of rules for computers to communicate over a network. Computer experts design HTTP to enable HTTP clients (such as Web browsers) to request information and services from an HTTP server (Web server) when a client makes a request to the server and then the Web server returns a response (response)

    • All HTTP connections are constructed as a set of requests and responses.

====

The HTTP communication mechanism is that during a complete HTTP communication, the following 7 steps will be completed between the Web browser and the Web server:

1. Establishing a TCP connection
    • Before HTTP work begins, the Web browser first establishes a connection to the Web server over the network, which is done through TCP, which works with the IP protocol to build the Internet, known as the TCP/IP protocol family, so the internet is also known as a TCP/IP network. HTTP is a higher level of application-level protocol than TCP, according to the rules, only the lower layer protocol is established before it can be a more protocol connection, so the first to establish a TCP connection, the port number of the general TCP connection is 80
2. Web browser sends request command to Web server
    • Once a TCP connection is established, the Web browser sends a request command to the Web server

Example: get/sample/hello.jsp http/1.1

3. Web browser sends request header information
    • After the browser sends its request command, it also sends some other information to the Web server in the form of header information, and then the browser sends a blank line to notify the server that it has ended sending the header information.
4. Web server Answer
    • After the client makes a request to the server, the server sends a reply back to the client,
HTTP/1.1 200 OK

The first part of the answer is the version number of the protocol and the Answer status code

5.Web server sends answer header information
    • Just as the client sends information about itself along with the request, the server also sends the user with the answer about its own data and the requested document.
6.Web server sends data to browser
    • After the Web server sends a header message to the browser, it sends a blank line to indicate that the header information is sent to the end, and then it sends the actual data requested by the user in the format described by the Content-type reply header information
7.Web server shuts down TCP connection
    • In general, once the Web server sends the request data to the browser, it closes the TCP connection and then if the browser or server joins this line of code in its header information
      Connection:keep-alive
      The TCP connection remains open after it is sent, so the browser can continue to send requests through the same connection. Maintaining a connection saves the time it takes to establish a new connection for each request and also saves network bandwidth.
HTTP request Format

When the browser makes a request to the Web server, it passes a block of data to the server, which is the request information, and the HTTP request information consists of 3 parts:

    • Request method URI Protocol/version
    • Requests header (Request header)
    • Request Body

The following is an example of an HTTP request:

GET/sample.jspHTTP/1.1Accept:image/gif.image/jpeg,*/*Accept-Language:zh-cnConnection:Keep-AliveHost:localhostUser-Agent:Mozila/4.0(compatible;MSIE5.01;Window NT5.0)Accept-Encoding:gzip,deflate username=jinqiao&password=1234

Interpretation:

1.1 Request Method URI Protocol/version

get/sample.jsp http/1.1

    • In the code above, "GET" represents the request method, "/sample.jsp" represents the URI, "http/1.1 represents the version of the Protocol and Protocol."
    • HTTP requests can use a variety of request methods, depending on the HTTP standard. For example: HTTP1.1 supports 7 methods of request: GET, POST, HEAD, OPTIONS, PUT, delete, and Tarce. In Internet applications, the most common method is get and post.

    • The URL completely specifies the network resource to be accessed, usually with a relative directory relative to the root of the server, and always begins with "/"
    • Finally, the protocol version declares the version of HTTP that is used during the communication process.

1.2 Requests header (Request header)
    • The request header contains many useful information about the client environment and the request body. For example, the request header can declare the language used by the browser, the length of the request body, and so on.

    • Accept:image/gif.image/jpeg. /
    • Accept-language:zh-cn
    • Connection:keep-alive
    • Host:localhost
    • user-agent:mozila/4.0 (Compatible:msie5.01:windows NT5.0)
    • Accept-encoding:gzip,deflate.

1.3 Request Body
    • Between the request header and the request body is a blank line, which is very important, which indicates that the request header has ended, followed by the request body. The request body can contain query string information submitted by the customer:

username=jinqiao&password=1234

In the HTTP request for the example above, the body of the request has only one line of content. Of course, in real-world applications, the HTTP request body can contain more content.

HTTP request method of GET, post one. Send data to a Web page 1. Get (with parametric: params)
    • Passing parameters in the form of key-value pairs
payload = {‘key1‘: ‘value1‘, ‘key2‘: [‘value2‘, ‘value3‘]}r = requests.get(‘http://httpbin.org/get‘, params=payload)
2. Post (with parameter: data)

The post is similar to the Get method, except that it is not params= ... But data= ...

Pass-through forms
payload = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘}r = requests.post("http://httpbin.org/post", data=payload)
Transmittal file (Files)
url = ‘http://httpbin.org/post‘files = {‘file‘: open(‘report.xls‘, ‘rb‘)}r = requests.post(url, files=files)
3. Seven methods of the requests library and 13 parameters
    1. Requests.request (): Constructs a request to support the basic methods of each method
    2. Requests.get (): Gets the main method of the HTML page, corresponding to the HTTP GET
    3. Requests.head (): Gets the HTML page header information method, corresponding to the HTTP head
    4. Requests.post (): Submit the Post request method to the HTML page, corresponding to the HTTP post
    5. Requests.put (): A method for submitting a put request to an HTML page, corresponding to the HTTP rut
    6. Requests.patch (): Submit a local modification request to an HTML Web page that corresponds to a patch for HTTP
    7. Requests.delete (): Submit a DELETE request to an HTML page that corresponds to an HTTP delete
    • HTTP protocol:
    1. Http,hypertext Transfer Protocol, Hypertext Transfer Protocol.
    2. HTTP is a stateless application-layer protocol based on "request and response" mode.
    3. The HTTP protocol uses URLs as the identifier for locating network resources.
    4. URL format: http://host[:p Ort][path]
    5. Host: A legitimate Internet host domain name or IP address
    6. Port: Port number, the default is 80
    7. Path: Paths to request resources
    8. URLs are Internet paths that access resources through the HTTP protocol, and a URL corresponds to a data resource.
    • 13 parameters
 requests.request(method,url,**kwargs)
    • Method: Request method, corresponding to Get/put/post and other 7 kinds
    • URL: URL link to get page
    • **kwargs: Control access parameters, total 13
    • **kwargs: The parameters that control access are optional:

====

13 parameters include:
    1. Params: Dictionary or sequence of bytes, added as a parameter to the URL
    • Data: Dictionary, byte sequence, or file object, as the content of the request
    • Data in Json:json format as content of request
    • Headers: dictionary, HTTP customization header (simulated browser access)
    • Cokies: A cookie in a dictionary or cppliejar,request
    • Auth: meta-ancestor, support HTTP authentication function
    • Files: dictionary type, transferring files
    • Timeout: Sets the time-out, in seconds
    • Proxies: Dictionary type, set Access Proxy, can increase login authentication
    • Allow_redirects:true//false, default true, redirect switch
    • Stream:true/false, the default is True, gets the content download now switch
    • Verify:true/false, default is True, authentication SSL certificate switch
    • Cert: Local SSL Certificate Path "" "

Cases:

kv={‘key1‘:‘value‘,‘key2‘:‘value2‘}r=requests.request(‘GET‘,‘http://python123.io/ws‘,params=kv)print(r.url)

Output Result:

http://python.io/ws?key1=value1&key2=value2

HTTP protocol ~ Two

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.