这几天一直在想,经过Serverless
Framework作一些什么事情,是有趣的,思前想后,决定作一个APICenter,基于腾讯云Serverless架构,使用Serverless
Framework只作一个API市场。php
简单又愉快的,创建一个Project,全程将会用Python3.6进行开发。html
同时,我也会将这个系列的项目开源到git
https://github.com/anycodes/S...github.comgithub
废话很少说,先来第一个有趣的API,获取用户IP和IP地址,经过搜索引擎,咱们能够查看到本身的IP地址:编程
咱们能够经过浏览器抓包,得到到这个请求的接口:json
经过对接口的精简,能够肯定,获取个人IP地址的请求连接就是:api
https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=192.168.1.1&co=&resource_id=6006
接下来,咱们能够经过Python编程来作这个功能:浏览器
# -*- coding:utf-8 -*- import urllib.request import json import uuid def return_msg(error, msg): return_data = { "uuid": str(uuid.uuid1()), "error": error, "message": msg } print(return_data) return return_data def get_ip_addr(ip): url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=%s&co=&resource_id=6006" % ip ip_data = json.loads(urllib.request.urlopen(url=url).read().decode("gbk")) if ip_data["data"] and len(ip_data["data"]) > 0: return ip_data["data"][0]["location"] else: return False def main_handler(event, context): try: user_ip = event["requestContext"]["sourceIp"] location = get_ip_addr(user_ip) if location: return return_msg(False, {"ip": user_ip, "location": location}) else: return return_msg(True, "未能正确得到到IP地址") except Exception as e: print(e) return return_msg(True, "内部错误") def test(): event = { "requestContext": { "serviceId": "service-f94sy04v", "path": "/test/{path}", "httpMethod": "POST", "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", "identity": { "secretId": "abdcdxxxxxxxsdfs" }, "sourceIp": "14.17.22.34", "stage": "release" }, "headers": { "Accept-Language": "en-US,en,cn", "Accept": "text/html,application/xml,application/json", "Host": "service-3ei3tii4-251000691.ap-guangzhou.apigateway.myqloud.com", "User-Agent": "User Agent String" }, "body": "{\"test\":\"body\"}", "pathParameters": { "path": "value" }, "queryStringParameters": { "foo": "bar" }, "headerParameters": { "Refer": "10.0.2.14" }, "stageVariables": { "stage": "release" }, "path": "/test/value", "queryString": { "foo": "bar", "bob": "alice" }, "httpMethod": "POST" } print(main_handler(event, None)) if __name__ == "__main__": test()
测试运行:架构
编写Serverless Framework的Yaml:app
get_user_ip: component: "@serverless/tencent-scf" inputs: name: myapi_get_user_ip codeUri: ./get_user_ip handler: index.main_handler runtime: Python3.6 region: ap-beijing description: 获取用户的IP相关信息 memorySize: 64 timeout: 2 events: - apigw: name: serverless parameters: serviceId: service-8d3fi753 environment: release endpoints: - path: /get_user_ip description: 获取用户的IP相关信息 method: GET enableCORS: true
我这里已经指定了一个APIGW,由于我已经提早创建了。接下来能够部署:
完成部署,能够看一下咱们的效果:
至此,咱们完成了IP查询的小工具,固然,若是用户主动查询IP地址呢?咱们能够对代码进行改造:
Serverless.yaml进行简单升级:
get_user_ip: component: "@serverless/tencent-scf" inputs: name: myapi_get_user_ip codeUri: ./get_user_ip handler: index.main_handler runtime: Python3.6 region: ap-beijing description: 获取用户的IP相关信息 memorySize: 64 timeout: 2 events: - apigw: name: serverless parameters: serviceId: service-8d3fi753 environment: release endpoints: - path: /get_user_ip description: 获取用户的IP相关信息 method: POST enableCORS: true param: - name: ip position: BODY required: 'FALSE' type: string desc: ip地址
再次部署:
部署完成以后:
测试1:
测试2:
至此,咱们完成了咱们的第一个API:查询IP地址