Original link: https://developer.aliyun.com/article/981268

Demo video: https://developer.aliyun.com/live/249772

1. Introduction to Serverless

Serverless, which means "serverless" in Chinese, does not mean that there is no need to rely on resources such as servers, but that developers no longer have to think too much about servers, and can focus more on product code while computing resources Also starting to appear as a service, rather than as a concept of a server.

1. Serverless Architecture

It mainly consists of two parts: Baas and Faas, which are usually located in the cloud and do not need to pay attention to the bottom-level server when using it.
Baas (Backend as a Service: Backend as a Service) includes object storage, cloud database, API gateway, message push, etc.
Faas (Functions as a Service) abstracts computing power to respond to events without managing servers.

2. Three major application scenarios of Serverless

Scenario 1: Event-triggered scenario, that is, it will only be executed when an event is triggered.
Scenario 2: In a traffic burst scenario, in the event of a sudden large traffic situation, the serverless architecture is used for on-demand loading, elastic scaling, resource saving, and load balancing.
Scenario 3: In the big data processing scenario, users only need to upload the core code to Function Compute to quickly complete the entire work.

3. Advantages of Serverless

Under the traditional architecture, in the face of large-traffic scenarios, it is necessary to add machines or upgrade machines, which makes operation and maintenance more difficult. In the face of peaks and troughs, it is impossible to use on-demand, and the cost is high.
Under the serverless architecture, developers only need to focus on code development, without the need to manually activate services and configuration management in each cloud resource console, and can automatically perform elastic scaling according to business requests; support users to pay on demand, with low cost; fast development cycle, It greatly improves the efficiency of development and deployment.
For details, please refer to Alibaba Cloud related documents https://developer.aliyun.com/group/serverless

2. Python implements sending emails

 import requests
import yagmail   # 此模块用于发邮件
import schedule  # 此模块用于计划任务
from bs4 import BeautifulSoup
import re

ran = 0
url = 'https://tianqi.2345.com/cixian1d/70177.htm'  # 定义天气预报的url
loveurl = 'https://www.guaze.com/juzi/23389.html'  # 定义情话的url
def email():
    global ran      # 将ran变量声明为全局变量
    web = requests.get(url)
    # print(web.text)

    page = BeautifulSoup(web.text,"html.parser")

    # print(ran)

    # print(love[ran])

    weather = page.find("div",class_="real-today")
    # print(weather.text)

    web2 = requests.get(loveurl)
    web2.encoding = 'gb2312'
    page = BeautifulSoup(web2.text, "html.parser")

    div = page.find('div', class_="content")

    div = str(div.text)
    # print(div)
    grep = re.compile(r"\d+、(.*)")
    content = grep.findall(div)
    # print(content)


# email函数内的内容是爬取天气和情话的,具体的地址天气你可以更换url

    yag = yagmail.SMTP(
        host='smtp.qq.com', user='xxxxxxx@qq.com',   # 如过用的是qq邮箱就写smtp.qq.com,如果是163就写smtp.163.com
        password='xhaztrwpjffpbdhh', smtp_ssl=True       # 授权码在qq邮箱里开启smtp就会生成一个
    )
    weather = [weather.text,"每日情话:",content[ran],    # 定义发送内容
               yagmail.inline(r"/.love.jpg")    # 附件图片,不发图片可以删掉
               ]
    yag.send(
        to=['xxxxxxxxx@qq.com'],
        subject='早鸭',           # 邮件主题
        contents=weather          # 发送的内容为上面定义的weather,其中weather.text是天气预报,content[ran]是情话
    )
    print("发送完成")
    ran += 1

schedule.every().day.at("05:21").do(email)      # 每天5点20分执行函数email0
#schedule.every(10).seconds.do(email)  #每10秒执行一下函数email的内容,我这里用于测试
while True:
    schedule.run_pending()

3. Deploy to Alibaba Cloud Serverless to achieve automatic sending

1. Log in to the Alibaba Cloud homepage

image-20220701202818857.png

2. Select Product->Elastic Computing->Serverless->Function Computing FC

image-20220701202913872.png

3. Enter the console -> Services and Functions -> Create Function

image-20220701203133969.png
image-20220701203309597.png
image-20220701203504468.png
image-20220701203546979.png
image-20220701203657754.png

4. Upload code -> upload folder -> select folder -> save and deploy

image-20220706181040181.png
image-20220706181236172.pngimage-20220706181318098.png
image-20220706181414524.png

5. Function Configuration -> Edit Environment Information -> Modify Function Entry

image-20220706182415671.pngimage-20220706182534216.png

6. Add triggers to achieve daily scheduled delivery

Trigger Management -> Create Trigger -> Timing Trigger -> Fill in Name and Specify Time
image-20220706184028003.pngimage-20220706183941738.png
image-20220706185307405.png

7. Import dependencies and deploy

First execute the following three commands in the terminal to import the dependencies required by the project

 pip3 install yagmail -t .
pip3 install schedule -t .
pip3 install bs4 -t .

image-20220706181742559.png
Click Save and Deploy in the upper right corner
image-20220706182019285.png

Fourth, the effect display

Click the test function in the upper left corner, and then view the running result through the real-time log.
image-20220706182226604.pngimage-20220706182732131.png
image-20220701211458910.png
image-20220706183510459.png

V. Summary

This actual combat is a deep understanding of Serverless, and while gaining technology, I also improve my learning ability. Since I am currently preparing for the postgraduate entrance examination, I haven't updated the article on self-learning technology for a long time. This time, I will also learn the technology and ideas of the hot serverless serverless architecture through the official evaluation activity of Alibaba Cloud. I will share this popular technology here. Then combined with some personal and simple exploration, I hope to learn and grow together with the big guys! 😄😄

For more content, pay attention to the Serverless WeChat official account (ID: serverlessdevs), which brings together the most comprehensive content of serverless technology, regularly holds serverless events, live broadcasts, and user best practices.


Serverless
69 声望265 粉丝