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 |
urls = ( |
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 calledIndex
The 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.py
Go 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 |
import web |
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./hello
The path will be determined by theHello
classGET
method 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 aGET
request to request web content. In web.py, theGET
method corresponds to processing theGET
Requested class methods. When the user accesses a URL, web.py calls the corresponding class’sGET
method 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 areJSON或XMLformat. Typically, the server will pass theContent-Type
Specifies 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}
- Get all users:
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 |