腾讯云

# -*- coding: utf8 -*-
from urllib import parse

def get_url(type='pivix'):
    pass

def main_handler(event, context):
    if event['httpMethod'] == 'GET':
        if 'type' in event['queryString']:
            imgtype = event['queryString']['type']
            url = get_url(imgtype)
        else:
            url = get_url()
        if url:
            return {'code': 0, 'url': url, 'msg': "成功"}
        else:
            return {'code': 1, 'msg': "失败"}
    if event['httpMethod'] == 'POST':
        body = parse.parse_qs(event['body'])
        print(body)
        if 'type' in body:
            imgtype = body['type'][0]
            url = get_url(imgtype)
        else:
            url = get_url()
        if url:
            return {'code': 0, 'url': url, 'msg': "成功"}
        else:
            return {'code': 1, 'msg': "失败"}

阿里云

# -*- coding: utf-8 -*-

import logging
import json
from urllib import parse

# To enable the initializer feature (https://help.aliyun.com/document_detail/158208.html)
# please implement the initializer function as below:
# def initializer(context):
#    logger = logging.getLogger()
#    logger.info('initializing')


def handler(environ, start_response):
    context = environ['fc.context']
    request_uri = environ['fc.request_uri']
    for k, v in environ.items():
        if k.startswith('HTTP_'):
            # process custom request headers
            pass
    # do something here
    try:
        request_body_size = int(environ.get('CONTENT_LENGTH', 0))
    except (ValueError):
        request_body_size = 0
    request_body = environ['wsgi.input'].read(request_body_size).decode(encoding="utf-8")
    body = parse.parse_qs(request_body)
    for i in body:
        body[i] = body[i][0]
    status = '200 OK'
    response_headers = [('Content-type', 'application/json')]
    start_response(status, response_headers)
    return [json.dumps(body).encode(encoding="utf-8")]
最后修改:2021 年 06 月 13 日 10 : 03 PM