Py学习  »  Python

azure对函数应用使用python烧瓶框架

wasp256 • 5 年前 • 512 次点击  

我看到azure现在在函数应用程序中支持python(preview)。我有一个现有的flask应用程序,想知道是否可以在没有重大更改的情况下将其部署为一个功能应用程序?

我已经阅读了在函数应用程序中使用python的azure教程( https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python )但是没有烧瓶框架…

有人有经验吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40558
 
512 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Peter Pan
Reply   •   1 楼
Peter Pan    6 年前

我尝试了不同的方法将azure的python函数与flask框架集成。最后,我在名为 TryFlask 通过 app.test_client() .

这是我的示例代码,如下所示。

import logging
import azure.functions as func
from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/hi')
def hi():
    return 'Hi World!'

@app.route('/hello')
@app.route('/hello/<name>', methods=['POST', 'GET'])
def hello(name=None):
    return name != None and 'Hello, '+name or 'Hello, '+request.args.get('name')

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    uri=req.params['uri']
    with app.test_client() as c:
        doAction = {
            "GET": c.get(uri).data,
            "POST": c.post(uri).data
        }
        resp = doAction.get(req.method).decode()
        return func.HttpResponse(resp, mimetype='text/html')

在本地和azure上测试时,访问url / ,‘嗨’ /hello 通过URL http(s)://<localhost:7071 or azurefunchost>/api/TryFlask 带查询字符串 ?uri=/ , ?uri=/hi ?uri=/hello/peter-pan 在浏览器中,并执行 POST 方法使用查询字符串访问上面的同一URL。 ?uri=/hello/彼得潘 ,这些都是工作。请将结果作为下面的本地数字查看,与云上的结果相同。

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

注意:在我的解决方案中,url必须是 http(s)://<localhost:7071 or azurefunchost>/<routePrefix defined in host.json, default is api>/<function name>?uri=<uri defined in app.route, like / or /hi or /hello, even /hello/peter-pan?name=peter> .