[Python Flask Practice] Get HTTP request data

[Python Flask Practice] Get HTTP request data

[[389990]]

When the client accesses the server program through the URL, it will send two types of information to the server, one is the HTTP request header, and the other is the request data.

Generally, HTTP requests submit data to the server through the GET method and the POST method. Therefore, the server program needs to obtain the request data from the client and then do further processing. For example, if the server wants to do some statistics on the type of client (what browser is used), it needs to obtain the value of the User-Agent field in the HTTP request header. If you want to get the data submitted by the client form, you need to obtain the data of the GET request or POST request on the server.

Reading POST requests will be introduced in detail in the following articles. This article will first look at how to read HTTP request headers and GET request data. To read HTTP request headers and GET request data in Flask, you need to import a global variable request in the flask module, and then use request.headers.get(...) to read HTTP request header data. The parameter of the get method is the name of the HTTP request header field. Use request.args.get(...) to read the value of a field in a GET request. The parameter value of the get method is the field name of the GET request.

This example writes two routes, one for reading HTTP request header data and the other for reading GET request data.

  1. from flask import Flask
  2. from flask import request
  3.   
  4. app = Flask(__name__)
  5. # Root route, used to read HTTP request header data
  6. @app.route( '/' )
  7. def index ():
  8. # Read the User -Agent field value of the HTTP request header
  9. user_agent = request.headers.get( 'User-Agent' )
  10. return   '<h1>Your browser is %s</h1>' % user_agent
  11. # Route for reading GET request data
  12. @app.route( '/abc' )
  13. def abc():
  14. # Read the arg field value in the GET request
  15. value = request.args.get( 'arg' )
  16. return   '<h1>arg = %s</h1>' % value
  17. if __name__ == '__main__' :
  18. app.run()

Run the program and enter the following URL in the browser address bar.

  1. http://localhost:5000

After accessing the above URL, the information shown in Figure 1 will be output in the browser. It should be noted that this output information will vary depending on the browser used by the reader. But it will describe the type of browser used by the reader. For example, this example uses the Chrome browser for testing, so the output information will show Chrome and the corresponding version number.

Figure 1 Returning the browser type

Next, enter the following URL in the browser address bar.

  1. http://localhost:5000/abc?arg=hello

The content shown in Figure 2 will be output in the browser.

Figure 2 Returning GET request parameter values

This article is reprinted from the WeChat public account "Geek Origin", which can be followed through the following QR code. To reprint this article, please contact the Geek Origin public account.

<<:  What will 6G look like in the future?

>>:  Do you know all the HTTP protocols?

Recommend

JustVPS New London VPS 30% off, $3.08/month - 1GB/20GB/300M unlimited traffic

JustVPS.pro recently launched a new VPS in London...

...

5G commercialization promotes the scale development of industrial Internet

The Industrial Internet is a network that connect...

Comparative Analysis of Kubernetes Network Plugins (Flannel, Calico, Weave)

[[269494]] This article will focus on exploring a...

ITU releases draft specification, a big step forward in 5G standardization

According to foreign media reports, despite the o...

Multicast Protocol: The "Group Chat Master" of the Internet World

Fans who love to think, have you ever had these c...

16 WiFi withdrawal triggers myths: Is public WiFi coming to an end?

Recently, 16WiFi, which provides free WiFi Intern...