In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to use Python to create short-chain services, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Longer and longer web sites and short-chain services
The faster the development of the Internet, the more functions, some web sites are also getting longer and longer, if only browsing, involving communication, a long list of web sites distribution is easy to make mistakes and not easy to promote. Moreover, some Internet giants strictly prevent competitors' links from appearing in their own products, using all means to restrict competitors' URL links from being opened and drained smoothly on their own products.
For the purpose of easy dissemination, not affecting the appearance of the page or bypassing the blocking, the URL shortening service arises at the historic moment.
At present, there are many websites in China that offer website shortening services, such as Tencent's t.im, Weibo's t.cn, Baidu's dwz.cn, and so on. Some of them only provide simple URL shortening services, while others have added visitor statistics on the basis of the URL shortening service, which is very useful for some promotion links.
The implementation of the short chain is very simple, and we can easily build a short chain service using Python.
Second, build short-chain services
There are generally two ways for URL shortening and short chain generation.
One is to use a hash algorithm, and the other is to generate a random string corresponding to the original URL and store it in the database.
Here, we use the latter method.
Short-chain service is a Web service after all, and we need a Web framework.
There are many excellent Web frameworks in Python, such as Flask, Django, Tornado.
Each framework has its own characteristics and advantages, and each framework has its own supporters. Here, we choose Django for no other reason, but are familiar with it.
1. Create a virtual environment
To isolate from the native Python environment, we use virtualenv to create an isolated virtual Python environment.
2. Install Django
Use the active command to enter the virtual environment and install the required Django libraries.
3. Create Django projects and applications
Use the django-admin tool to create a project (project) dwz and an application (app) shortener
Let's start the project and see if it's right.
Start the project
Well, start the normal
Next, we create a database model that holds the original URL and random strings
4. Analyze and create a database model
Because we choose the URL shortening scheme in which random strings match the original URL, we need to use the database to store the generated random string and the original URL one by one in a data table.
Analyze what fields there are in this data table:
Random string: the shortening code corresponding to the original URL
Original URL URL
Creation time: convenient for recording and statistics
Activity status: convenient short-chain management
Then, we need to create a data model with four fields in Django.
Create a DwzURL model in the models.py file:
Add the application to the INSTALLED_APPS of setting.py:
Generate the model:
When you are finished, run the project and go to the background to have a look.
Create a superuser first:
Log in to the background:
It was found that there was no model we created, and it turned out that we forgot to add it to admin.py:
We add the model in admin.py. By the way, the English interface doesn't seem to fit. By the way, change the language to Chinese in the setting.py file.
Add a model to admin.py:
Change the language of the website to Chinese:
Let's go backstage and have a look:
Well, our new model has appeared, and the language of the website has changed from English to Chinese.
Let's create a new record and take a link to the previous Taobao activity:
Well, our new record also appeared backstage:
The correspondence is established, but this is manual. What we need is to enter a long URL and generate a shortening code.
5. Optimization model method.
Well, we create a function that generates a shortening code, and then rewrite the save () method of the DwzURL model so that it automatically generates a shortening code when it enters a URL.
Create a shortening code generation function to randomly generate a 6-digit string from English letters and numbers:
Override the save () method of the DwzURL model to automatically call the shortened code generation function to generate a shortened code when it is saved:
Let's test it, add a new record and enter only the URL URL:
As a result, a shortening code was successfully generated as we expected:
In this way, we expect to enter only the URL and automatically generate a shortened code at random.
6. Create a view
Django is a typical Web framework of MVC pattern. The model (models) is responsible for data recording, view (views) is responsible for business logic, routing (urls) is responsible for the page path corresponding to business logic, and the final template (templates) is responsible for page rendering.
Now that we have built the model, let's write the business logic (view) of URL shortening.
At present, the logic of our whole service is very simple:
Three functions: home page display, shortened code generation, short chain jump
Two views: home page (GET method presents home page) and short code generation view (POST method generates shortening code), short chain jump view
Let's do it!
The first is the first view of the home page:
Create a class-based view, create a get method to render the home page HTML;, create a post method, receive the URL passed by the form, generate a shortening code and return:
Create a second short-chain jump view, get the shortening code for comparison from the database, redirect to the original URL if it exists, and return 404 if it does not exist:
Now that the view is created, set up the route.
7. Set up routing
According to the view we wrote, we need to add two new routes:
Home page
Jump
Add two new routes to the urlpatterns list in urls.py, corresponding to two views:
The routing is also set up, and by the way, we are also missing the HTML template. Search the Internet for a template for a search box:
Well, that's it!
Create a template directory templates, and set the template path in settings.py:
Create a static directory and set the static file path in the settings file:
8. Testing
Since we are testing locally, we need to modify the local hosts file to map a short domain name to 127.0.0.1. If it is deployed on the server, bind the domain name and deployment directly.
Map g.me to local port:
Open the home page and take a look:
Let's enter a long URL to see (gif motion picture):
Well, in this way, a short-chain service with basic functions has already been constructed using Python.
After reading the above, have you mastered how to use Python to create short-chain services? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.