[Tutorial] Get to know cloud functions for the first time, and realize cloud-based projects without servers!

Please indicate the source when reprinting: Senior Xiaofeng's Big Bang Life [xfxuezhang.cn]

Are you also worried and want to run your project on the cloud, but don't want to spend a lot of money to buy cloud servers?

Introduction to Cloud Functions

The simple understanding of         a cloud function (Serverless Cloud Function, SCF ) is: a project that can be deployed on the cloud , but does not require a special purchase of a server , and can be triggered to run through specific methods such as "scheduled/url access" . This is especially suitable for users who need a cloud environment but don't need to buy a server at such a large cost.

        For example, I just want to regularly run a script that checks patent status and automatically signs in, or maybe I want to run a flask API network manager, and you can even use it to do network verification for homebrew.

Example of use

        Here, we briefly introduce a small application based on Tencent Cloud functions : regular monitoring of patent status. The deployment process is the same. After you are familiar with it, you can try to expand from small applications to large applications.

Receive cloud function resources

At present, Tencent is giving away a 3-month trial version         to new users for free , and students who do not have cloud function resources can hurry up! Receive the entrance: free cloud function

script writing

The writing of the script is exactly the same as the mode developed locally, there is no difference.

1. First click " New " cloud function:

2. The official here also provides a lot of routines for you to try directly:

3. However, if we create it ourselves, we should choose " From Scratch " on the right and fill in the information:

The " event function " and " web function "         here are your trigger execution methods . Since it is executed at regular intervals, choose "event function" here. If you want to trigger execution (such as uploading pictures) by accessing a specified URL, then select "web function".

        " Advanced configuration " here is filled in according to your code requirements, generally speaking, just keep the default:

        " Trigger configuration " here is to let us set the timing information:

        Note that if you selected " web function " earlier, the content here is different. It looks like this, which is more intuitive and easy to understand:

4. After the deployment is completed, you can start writing specific codes. The IDE should be changed by vscode, is it exactly the same as your local one:

        If you finish writing the code, you can click " Test " to run it, and the output will be displayed in the lower right corner:

5. Let's take this patent status as an example: SooPAT patent search , it looks like this:

        We use the requests library to request this url, and then parse its returned content. Note that the requests here are third-party libraries , how can this be installed?

We can directly execute the pip installation in the " terminal "         under the IDE , which is very convenient:

        Then, the final code can be like this:

# -*- coding: utf8 -*-
import requests
from lxml import etree

def main_handler(event, context):
    url = 'http://www.soopat.com/Home/SipoLegal/202211296680'
    html = requests.get(url)
    root  = etree.HTML(html.text)
    date = root.xpath('//*[@id="PatentContentTable"]/tbody/tr[3]/td[2]/text()')[0].strip()
    state = root.xpath('//*[@id="PatentContentTable"]/tbody/tr[5]/td[2]/text()')
    state = ', '.join(state).strip()
    content = '公告日: ' + date + '; 状态: ' + state
    url = 'http://xfxuezhang.cn:9966/QQ/send/?target=1061700625&msg='+content
    requests.get(url)

        This code implements checking the patent status and sending the result to the specified url.

6. After confirming that there is no problem with the code, click " Deploy " below to let the code start to execute automatically:

7. Support the monitoring of various information to facilitate the reasonable allocation of resources:

Serverless application

        If the cloud function described above is a function-level migration to the cloud, then a serverless application is a framework-level migration to the cloud, which can help you create an environment under a specified framework without manual construction. As you can see, the new interface only provides a choice of various frameworks:

        This type is suitable, for example, if you want to make an API gateway, or a specific and complete application. Let's give you a demonstration by creating a Flask gateway .

1. Simply fill in the information:

        In " Advanced Configuration ", we can check the " Fixed Export IP " option, because it is free , so you don't have to use it for nothing!

2. It will automatically pull the code and initialize the environment. Depending on the size of the project, it may take 5 minutes to wait silently for the deployment to complete:

3. After the creation is complete, click the " URL " to access the application, but we haven't written the code yet. Let's go back to the " Function Service " column, you can see the flask application we created, click in:

        As in the previous steps, we write the code according to our own needs, and click " Deploy " after writing.

Summarize

        The cloud function is really very convenient, it helps us realize the cloud of the project, and does not need to spend a lot of money to buy a cloud server. And another point is that the communication security of the cloud function is guaranteed by Tencent , so it eliminates the need to consider how to prevent intrusion on the server. It is simply convenient for users with little demand.

Guess you like

Origin blog.csdn.net/sxf1061700625/article/details/132241997