[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

TCP waves four times: Why four times? The principle is revealed!

introduction Hello, everyone. I am your technical...

The so-called ICMP is nothing more than a general and soldiers

[[396354]] This article is reprinted from the WeC...

Why TCP will be replaced by UDP

Why's THE Design is a series of articles abou...

5G healthcare development direction and challenges

[[403098]] The development of 5G healthcare has e...

The first interpretation in China: the potential game-changer behind IIoT

If the Industrial Internet is to be implemented, ...

Interesting explanation of bearer: PTN and IPRAN in one article

The "old-fashioned" old boss—SDH The tr...

Top ten trend predictions: Where will domestic telecom operators go in 2021?

After a complicated 2020, the wheel of time has e...

A brief introduction to intent-based networking (IBN)

In campus networks, there are many emerging trend...

Zhaorong Tribe wishes you a happy new year! Good luck in the Year of the Tiger!

On the occasion of the Chinese New Year, the trib...

The basics of optical fiber you must know

1. Classification of optical fiber Optical fibers...