What are we talking about when we talk about using http for front-end and back-end interaction? The most important part of any website is its url structure. In web.py, how do we build the url structure of a website? It’s just a matter of defining a tuple. 1 2 3 urls = ( ‘/’, ‘Index’ ) Oh I get it probably, the first element corresponds to where this url connection is located, that’s the url? It’s the equivalent of the directory of this website. The second one is the class mapped in Python. Send the http request to the website, Python how to deal with, is the need to develop the corresponding back of the class method to reflect it. Like the above urls correspondence, so long that we want the URL / (home) by a class called Index processing. So how exactly does web.py build this web application up? It’s all about this.1 app = web.application(urls, globals()) This command tells web.py to create an application based on the list of URLs that we’ve just submitted.This application looks up the corresponding class in the global namespace of this file. So in the web.py package, this so-called application is actually a program that handles external requests and handler interactions, and this web.application is the engine of the web application that’s going to be running all the time, and it’s going to handle the URL request, call the appropriate handler functions, and return the response. So really the key part I need to develop is the implementation of each of the classes in urls that are responsible for handling specific HTTP requests and defining the behavior of the application. In web.py, the urls variable is responsible for defining the mapping between paths and the classes and functions that handle those paths. Each path will correspond to a class, and web.py will call the corresponding processing logic based on the URL of the request.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import web # Definitions class Hello: def GET(self): return “Hello, world!” class Goodbye: def GET(self): return “Goodbye, world!” # Define the URL mapping urls = (“/hello”, “Hello”, # /hello path corresponds to the Hello class “/goodbye”, “Goodbye” # /goodbye path corresponds to the Goodbye class ) # Create a web.application instance, passing in the URL mapping app = web.application(urls, globals()) # Start the application if __ name__ == “__main__”: app.run() There are two more things to understand here. The user can access the paths defined in the urls, but these paths do not point directly to files or directories on the PC, these directories are just logical paths, not actual directories in the file system. This means that these paths do not directly correspond to a folder or file on disk, but are handled by Python classes and methods that are written. For example, the /hello path would be handled by the GET method of the Hello class, which is defined in memory. Essentially, the access or http protocol these ah, are sending a request and then returning a class. By visiting a URL and accessing it, the browser is actually sending a GET request to request the content of the web page. In web.py, the GET method corresponds to the class method that handles the GET request. When a user accesses a URL, web.py calls the GET method of the corresponding class to generate and return a response. a POST request is used to submit data. a PUT request is used to update a resource. a DELETE request is used to delete a resource. a RESTful API is a web and service interface based on the REST (Representational State Transfer) architecture style. RESTful API is a web and service interface based on the REST (Representational State Transfer) architectural style, an architectural style proposed by Roy Fielding in his PhD thesis. It is a design philosophy, not a protocol or technical specification.REST emphasizes the use of the Web’s basic functionality (e.g., HTTP methods) to design simple, flexible Web applications.The core principles of REST include: statelessness, each request must contain all the information needed to complete the request, and the server does not store any client state. Each request is independent of the client-server architecture, the separation of duties between the client and the server Unified interface for interaction through HTTP requests and responses Cacheability, the server can declare whether or not the response can be cached through the response header information Hierarchical system on-demand code, the server can provide executable code in the response to allow the client to dynamically load as needed RESTful APIs are API that conforms to the REST architectural style, which utilizes HTTP requests to manipulate resources. In a RESTful API, a resource is an entity in an application that can be identified, usually by a URL. For example, users, articles, products, etc. can be resources. This concept of resource identifiers is interesting, meaning that each resource has a unique URL, for example GET /users/123: get information about the user with ID 123 POST /users: create a new user PUT /users/123: update the information about the user with ID 123 DELETE /users/123: delete the ID 123 of the user. Statelessness: each request is independent and the server does not save any state of the client. The client needs to provide all the necessary information (such as authentication information, request data, etc.) in each request. Support for different response formats: RESTful APIs can return response data in a variety of formats, the most common being JSON or XML. Typically, the server specifies the response format via Content-Type in the response header.The most important design principle of a RESTful API is that it is resource-centric: in a RESTful API, the focus is on the resource, not the operation. Resources are identified by URLs. HTTP method operations should also match the verbs: GET: Get Resource POST: Create Resource PUT: Update Resource DELETE: Delete Resource Unified URL Formatting: URLs should be canonical, avoiding complex and inconsistent path design. Resources are usually represented in plural form. Example: Get all users: GET /usersGet individual users: GET /users/{id}Create new user: POST /usersUpdate user information: PUT /users/{id}Delete user: DELETE /users/{id}Hierarchical design: RESTful APIs should be designed to support a hierarchical structure that represents the relationships between resources through a logical URL design to represent the relationship between resources. Example: Get all the posts of a user: GET /users/{id}/posts To define the interface, then it is possible to list a table: Operation Request URLHTTP Method Description Get all users/usersGET Get a list of all users Get a single user/users/{id}GET Get the details of a user of the specified ID Create new user/usersPOST creates a new userUpdate user information/users/{id}PUT updates information about a user with the specified IDDelete user/users/{id}DELETE deletes a user with the specified IDGet the user’s posts/users/{id}/postsGET gets all the posts for the specified userCreate posts/users/{id}/postsPET gets all the posts for the specified userCreate posts/users/{id}/postsPATH id}/postsPOSTCreate a new post for the specified user

web.py体验

What are we talking about when we talk about using http for front-end and back-end interaction?

The most important part of any website is its URL structure. In web.py, how do we build the url structure of a website? It’s just a matter of defining a tuple.

1
2
3
urls = (
'/', 'Index'
)

Oh I get it probably, the first element corresponds to where this url connection is located, which is the url? which is equivalent to the directory of this website. The second is the class that is mapped in Python. Send the http request to the site, Python how to deal with, that is, you need to develop the corresponding back of the class method to reflect on it.

Like this urls correspondence above, then long means we want the URL/(home page) to be used by a URL calledIndexThe class handling of the

So how exactly does web.py get this web application built?

That’s what I’m talking about.

1
app = web.application(urls, globals())

This command tells theweb.pyGo ahead and create an application based on the list of URLs we just submitted. this application will look up the corresponding class in the global namespace of this file.

So in the package web.py, this so-called application, in fact, is to deal with external requests and processing methods to interact with a program, this web.application is the engine of the Web application, he has to run all the time, it handles the URL request, call the appropriate handler function, and return the response.

So really the key part I need to develop is the implementation of each of the classes in urls that are responsible for handling specific HTTP requests and defining the behavior of the application.

In web.py, the urls variable is responsible for defining the mapping relationship between paths and the classes and functions that process those paths. Each path will correspond to a class, and web.py will invoke the corresponding processing logic based on the requested URL.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import web

# 定义处理请求的类
class Hello:
def GET(self):
return "Hello, world!"

class Goodbye:
def GET(self):
return "Goodbye, world!"

# 定义URL映射
urls = (
"/hello", "Hello", # /hello 路径对应 Hello 类
"/goodbye", "Goodbye" # /goodbye 路径对应 Goodbye 类
)

# 创建 web.application 实例,传入 URL 映射
app = web.application(urls, globals())

# 启动应用
if __name__ == "__main__":
app.run()

There are two other issues to understand here, the user can access the paths defined in urls, but these paths are not directly pointing to files or directories on the PC, these directories are just logical paths, not actual directories in the file system. This means that these paths do not directly correspond to a folder or file on disk, but are handled by the Python classes and methods that are written. For example./helloThe path will be determined by theHelloclassGETmethod to handle it, and the class is defined in memory.

Essentially, access or http protocol these ah, are sending a request and then return a class.

By visiting a URL and accessing it, the browser is actually issuing aGETrequest to request web content. In web.py, theGETmethod corresponds to processing theGETRequested class methods. When the user accesses a URL, web.py calls the corresponding class’sGETmethod to generate and return a response.

POST requests are used to submit data.

PUT requests are used to update resources.

DELETE requests are used to delete resources.

RESTful API is a Web and service interface based on REST (Representational State Transfer) architecture style.

REST is an architectural style proposed by Roy Fielding in his doctoral dissertation. It is a design philosophy, not a protocol or technical specification.REST emphasizes the use of the Web’s basic functionality (e.g., HTTP methods) to design simple, flexible Web applications.

The core principles of REST include:

  • Statelessness, each request must contain all the information needed to complete the request, the server does not store any client state. Each request is independent of the
  • Client-server architecture, separation of client and server responsibilities
  • Unified interface to interact via HTTP requests and responses
  • Cacheability, where the server can declare whether the response can be cached or not via the response header information
  • hierarchical system
  • On-demand code, where the server can provide executable code in the response for the client to load dynamically as needed

A RESTful API is an API that conforms to the REST architectural style and utilizes HTTP requests to manipulate resources.

In a RESTful API, a resource is an entity in an application that can be identified, usually by a URL. For example, users, articles, products, etc. can be resources.

What’s interesting about this concept of resource identifiers is that it means that each resource has a unique URL, for example

GET /users/123: Get information about the user with ID 123

POST /users: Create a new user

PUT /users/123: Update user information with ID 123

DELETE /users/123: Delete the user with ID 123.

statelessness: Each request is independent and the server does not save any state of the client. The client needs to provide all the necessary information (e.g. authentication information, request data, etc.) in each request.

Support for different response formats: RESTful APIs can return response data in a variety of formats, the most common of which areJSONXMLformat. Typically, the server will pass theContent-TypeSpecifies the response format.

The most important design principle of the RESTful API is that theresource-centered: In a RESTful API, the focus is on resources, not operations. Resources are identified by URLs.

The operation of the HTTP method should also match the verb:

GET: Access to resources

POST: Creating Resources

PUT: Update resources

DELETE: Delete resources

Unified URL format: URLs should be normalized to avoid complex and inconsistent path design. Plural forms are often used to represent resources.

  • Example:
    • Get all users:GET /users
    • Get a single user:GET /users/{id}
    • Create a new user:POST /users
    • Update user information:PUT /users/{id}
    • Delete the user:DELETE /users/{id}

hierarchical design: The RESTful API design should support a hierarchical structure that represents the relationships between resources through sound URL design.

  • Example: Get all posts of a user:GET /users/{id}/posts

To define interfaces, then it is possible to list a table:

manipulate Request URL HTTP Methods descriptive
Get all users /users GET Get a list of all users
Getting a single user /users/{id} GET Get the details of a user with the specified ID
Create New User /users POST Create a new user
Updating user information /users/{id} PUT Update information for a user with a specified ID
Delete User /users/{id} DELETE Deletes the user with the specified ID
Get user’s articles /users/{id}/posts GET Get all posts of a given user
Create Article /users/{id}/posts POST Creates a new article for a specific user