SoFunction
Updated on 2025-03-03

How to get url through Flask's request object

Get url through Flask's request object

During the development of flask, sometimes you need to obtain the relevant data of the current requested url, so you can obtain the url in the following way! !

flask test request address: http://127.0.0.1:5000/main/index?page=10

Examples are as follows:

from flask import request
@('/main/index', methods=['GET', 'POST'])
@login_required
def index():
	host_url = request.host_url

Request More methods

  • : /main/index
  • : 127.0.0.1:5000
  • .host_url: http://127.0.0.1:5000/
  • .full_path: /main/index?page=10
  • .script_root:
  • : http://127.0.0.1:5000/main/index?page=10
  • .base_url: http://127.0.0.1:5000/main/index
  • .url_root: http://127.0.0.1:5000/

Request object, response object, session object

Request object

  • Function: Obtain information provided by users
  • Syntax: It is an instance that implements the ServletRequest interface class.

Use a form to submit information to a JSP page of the server

Common methods:

  • Set encoding method: For example (used to prevent Chinese characters from being garbled)
(“gb2312”);

Same effect:

String str = (“message”);
byte[] b = (“ISO-8859-1”);
Str = new String(b);
  • getProtocol(): The protocol used to obtain submission information
  • getServletPath(): Get the directory of the requested JSP page file
  • getContextPath(): Get the requested current web service directory
  • getContentLength(): Get the length of the submitted information
  • getMethod(): Methods to obtain submission information, such as post, get
  • getHeader(s : String): Get the value of the header name specified by parameter s in the http header file.

s can be: accept, accept-language, content-type, accept-encoding, user-agent, host, content-length, connection, cookie

  • getHeaderNames(): Get the enumeration type of the header name
  • getHeaders(s : String): Get an enumeration of all values ​​of the specified header name in the header file
  • getRemoteAdr(): Obtain the user's IP address
  • getRemoteHost(): Get the user's user machine name
  • getServletPort(): Get the server number
  • GetParameterNames(): Get an enumeration of the name parameter value in the submission information

Response object

Function: Make a dynamic response to user's request and send data to user

For example: The page directive specifies a value for contentType to determine the response MIME type.

If you want to change this value dynamically, you can use:

(s : String);   

The value of s can be: text/jtml; text/plain; image/gif; image/x-xbitmap; image/jpeg; image/pjpeg; application/x-shockwave-flash; application/-powerpoint; application/vnd-ms-execel; application/msword, etc.

Both the request and the response contain some method headers

use:

(head : String, value : String);
or
(head : String, value : String);

Dynamically add new responses and header values

For example:

(“Reflesh”, “5”);

Response redirection every 5 seconds

  • Syntax: (url: URL); response status line
  • Syntax: (n: int);

session object

Note: Tomcat is responsible for creating objects that are used to store various information submitted by users during access to the server page. Each object will be assigned a String type ID number (can be obtained using();) and stored in a cookie. The same user's sessions are different in different text service directories.

Implement the uniqueness of the session object: If the general user supports cookies, the uniqueness of the object has been implemented.

If it is not supported, you can realize the uniqueness of the session object by rewriting the URL. Call the encodeURL(id: String) or encodeRedirectURL(id: String) methods, add parameters to the new URL, and pass the id of the session object over.

  • setAttribute(key : String, obj : Object): void  Storage objects, add indexes
  • getAttribute(key : String): Object  Use index to get the object
  • getAttributeNmaes() : Enumeration generates an enumeration object, you can use nextElems() to traverse the keywords corresponding to each object in the session
  • removeAttribute(key : String): void Remove the object corresponding to the keyword
  • getCreationTime(): long  get the time of session creation (milliseconds)
  • getLastAccessedTime(): long  session last time it was/operated (milliseconds)
  • getMaxInactiveInterval(): int   Get the longest time of daytime (seconds)
  • setMaxInactiveInterval(interval : int)Set the longest time to daydream (seconds)
  • isNew(): boolean  Judge whether the session is the latest created
  • invalidate(): void   Invalid session

The survival cycle of session:

1. Call the invalidate() method to invalidate the session

2. The maximum time to set is set

3. Shut down the server

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.