在此案例中,使用阿里雲服務器。(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