在此案例中,使用阿里云服务器。(Linux)html
使用官方安装脚本自动安装python
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
复制代码
npm install @alicloud/fun -g
复制代码
fun config
复制代码
1\. 建立一个文件夹,如image_crawler
2\. 建立一个文件template.yml,内容
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
localdemo:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'local invoke demo'
image-crawler:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: index.handler
CodeUri: code/
Description: 'Hello world with python2.7!'
Runtime: python2.7
3\. 建立一个code文件夹,在文件夹中编写helloworld代码
def handler(event, context):
return 'hello world!'
4\. 运行函数
fun local invoke image-crawler
复制代码
import logging
import json
import urllib
logger = logging.getLogger()
def handler(event, context):
logger.info("event: " + event)
evt = json.loads(event)
url = evt['url']
html = get_html(url)
logger.info("html content length: " + str(len(html)))
return 'Done!'
def get_html(url):
page = urllib.urlopen(url)
html = page.read()
return html
复制代码
echo '{"url":"http://huaban.com/search/?q=%e5%a3%81%e7%ba%b8"}' | fun local invoke image-crawler
复制代码
def get_img(html):
reg = r'https:\/\/[^\s,"]*\.jpg'
imgre = re.compile(reg)
return re.findall(imgre, html)
复制代码
def handler(event, context):
logger.info("event: " + event)
evt = json.loads(event)
url = evt['url']
html = get_html(url)
img_list = get_img(html)
logger.info(img_list)
return 'Done!'
复制代码
EnvironmentVariables:
OSSEndpoint: oss-cn-hangzhou.aliyuncs.com
BucketName: fun-local-test
复制代码
endpoint = os.environ['OSSEndpoint']
bucket_name = os.environ['BucketName']
复制代码
creds = context.credentials
if (local):
auth = oss2.Auth(creds.access_key_id,
creds.access_key_secret)
else:
auth = oss2.StsAuth(creds.access_key_id,
creds.access_key_secret,
creds.security_token)
bucket = oss2.Bucket(auth, endpoint, bucket_name)
复制代码
count = 0
for item in img_list:
count += 1
logging.info(item)
# Get each picture
pic = urllib.urlopen(item)
# Store all the pictures in oss bucket, keyed by timestamp in microsecond unit
bucket.put_object(str(datetime.datetime.now().microsecond) + '.png', pic)
复制代码
import logging,datetime
import json,re,oss2
import requests,urllib,os
logger = logging.getLogger()
endpoint = os.environ['OSSEndpoint']
bucket_name = os.environ['BucketName']
local = 1
def handler(event, context):
logger.info("event: " + event)
evt = json.loads(event)
url = evt['url']
print(url)
creds = context.credentials
if (local):
auth = oss2.Auth(creds.access_key_id,creds.access_key_secret)
else:
auth = oss2.StsAuth(creds.access_key_id,creds.access_key_secret,creds.security_token)
bucket = oss2.Bucket(auth, endpoint, bucket_name)
html = get_html(url)
img_list = get_img(html)
logger.info(img_list)
iii(img_list,bucket)
return 'Done!'
def iii(img_list,bucket):
count = 0
for item in img_list:
count += 1
logging.info(item)
pic = urllib.urlopen(item)
if pic != None:
bucket.put_object(str(datetime.datetime.now().microsecond) + '.jpg', pic)
def get_img(html):
reg = r'http:\/\/[^,"]*\.jpg'
imgre =re.compile(reg)
return re.findall(imgre,html)
def get_html(url):
page = requests.get(url)
html = page.content
return html
复制代码
此时运行以下命令可看到成功效果。git
echo '{"url":"http://huaban.com/search/?q=%e5%a3%81%e7%ba%b8"}' | fun local invoke image-crawler
复制代码
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
localdemo:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'local invoke demo'
Policies: AliyunOSSFullAccess
image-crawler:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: index.handler
CodeUri: code/
Description: 'Hello world with python2.7!'
Runtime: python2.7
EnvironmentVariables:
OSSEndpoint: oss-cn-hangzhou.aliyuncs.com
BucketName: fun-local-test
复制代码
定时任务,如爬虫类。(图片转存、文字转存等)github
pipeline类别,如接受A信息,转存到B信息及格式。根据if,else等来分类的任务。web
微信,钉钉等公众号的后端(Flask),免去搭建服务器。docker
能够一个函数就解决的任务与项目,特别简单的项目。npm
阿里云官方函数计算后台提供Flask-web类型的函数计算模块,能够快速搭建外网api可访问的接口与页面。json