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. 123 urls = (‘/’, ‘Index’) Oh I […]

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 Read More »