在Azure API上托管Flask(python)API [英] Hosting Flask(python) API on Azure API

查看:87
本文介绍了在Azure API上托管Flask(python)API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

团队,我一直在尝试在Azure API应用程序上托管Flask API.而且它不起作用.

Azure API应用正在运行.托管页面打开​​,Python版本为3.6.6.

我无法托管简单的hello world项目,并且遇到的不同错误如下.

  1. 无法在web.config中添加处理程序.如果加上500错误.

  2. 无法从Kudu安装烧瓶.获取权限错误.与--user一起安装时,flask将安装到用户文件夹中.更改了路径以包括此文件夹,但仍然无法导入flask.

  3. 使用git部署.上传成功,但是在部署时出现错误,指出python 3.6不受支持的运行时版本.

可能我缺少在Azure API应用上设置python的基本知识吗?问题是我总是收到500错误.但是基本的azure托管起始页可以正常工作(除非我触摸web.config)

注意:-遵循了婴儿python API教程.

  • 按照天蓝色帮助部分中给出的说明进行操作

  • 还添加了python 3.6扩展名.什么都没改变.

解决方案

有类似SO线程的答案

  • 移至Kudu CMD控制台,

  • 然后按照以下命令在Kudu控制台中升级 pip 并安装 flask .

      D:\ home \ python364x64> python -V的Python 3.6.4D:\ home \ python364x64> pip -V来自D:\ home \ python364x64 \ lib \ site-packages(python 3.6)的pip 9.0.1D:\ home \ python364x64> python -m pip install --upgrade pip收集点正在下载https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl(1.3MB)安装收集的软件包:pip找到现有的安装:pip 9.0.1卸载pip-9.0.1:成功卸载了pip-9.0.1成功安装pip-18.1D:\ home \ python364x64> pip安装瓶收集瓶正在下载https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl(91kB)收集click> = 5.1(来自烧瓶)正在下载https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl(81kB)收集Jinja2> = 2.10(从烧瓶中收集)正在下载https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl(126kB)收集Werkzeug> = 0.14(从烧瓶中收集)正在下载https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl(322kB)收集其危险> = 0.24(从烧瓶中收集)正在下载https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl收集MarkupSafe> = 0.23(来自Jinja2> = 2.10->烧瓶)正在下载https://files.pythonhosted.org/packages/9d/80/9a5daf3ed7b8482e72ee138cef602b538cfba5c507e24e39fb95c189b16b/MarkupSafe-1.1.0-cp36-cp36m-win_amd64.whl安装收集的软件包:click,MarkupSafe,Jinja2,Werkzeug,其危险,flask脚本flask.exe安装在不在PATH上的'D:\ home \ python364x64 \ Scripts'中.考虑将这个目录添加到PATH,或者,如果您不想显示此警告,请使用--no-warn-script-location.成功安装Jinja2-2.10 MarkupSafe-1.1.0 Werkzeug-0.14.1 click-7.0 flask-1.0.2 itsdangerous-1.1.0 

    1. 上传 app.py & web.config 文件,

    它们的内容如下.

    app.py 内容:

    从烧瓶导入烧瓶的

     app = Flask(__ name__)@ app.route("/")def hello():返回"Hello World!" 

    web.config 内容:

     <?xml version ="1.0" encoding ="utf-8"?><配置>< appSettings><添加key ="PYTHONPATH" value ="D:\ home \ site \ wwwroot"/>< add key ="PATH" value ="D:\ home \ python364x64; D:\ home \ python364x64 \ Scripts;%PATH%"/></appSettings>< system.webServer><处理程序><添加名称="httpPlatformHandler" path ="*"动词="*" modules ="httpPlatformHandler" resourceType =未指定"/></handlers>< httpPlatform processPath ="D:\ home \ python364x64 \ Scripts \ flask.exe" arguments ="run --port%HTTP_PLATFORM_PORT%" stdoutLogEnabled ="true" startupRetryCount ='10'></httpPlatform></system.webServer></configuration> 

    然后重新启动浏览器或首先重新启动浏览器的API App,如下图所示.

    Team, I have been trying to host flask API on Azure API app. And it's not working.

    Azure API app is working. Hosting page opens up with python version as 3.6.6.

    I am not able to host simple hello world project and different errors I am getting are as follows.

    1. Can't add handlers in web.config. if added 500 error.

    2. Can't install flask from kudu. Getting permission error. When installed with --user, flask is installed to user folder. Changed path to include this folder but still can't import flask.

    3. Deployed using git. Upload is successful but while deploying, error stating unsupported run time version of python 3.6.

    Probably I am missing very basics of setting up python on Azure API app ? Problem is I am always getting 500 error. But basic azure hosting start page works ( unless I touch web.config)

    Note : - Followed baby python API tutorial.

    • Followed instructions given in azure help section

    • Also added python 3.6 extension. Nothing changed.

    解决方案

    There is an answer for a similar SO thread How to run python3.7 based flask web api on azure to help using WSGI_Handle to deploy your flask app.

    Or if you just want to know how to run a hello world demo in the offical flask website on Azure as like using flask run on local machine, you can follow my steps below.

    1. Install Python 3.6.4 x64 site extension via Kudu on your API App.

    2. Move to Kudu CMD console,

    Then to follow the below commands to upgrade pip and install flask in the Kudu console.

    D:\home\python364x64>python -V
    Python 3.6.4
    
    D:\home\python364x64>pip -V
    pip 9.0.1 from D:\home\python364x64\lib\site-packages (python 3.6)
    
    
    D:\home\python364x64>python -m pip install --upgrade pip
    Collecting pip
      Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
    Installing collected packages: pip
      Found existing installation: pip 9.0.1
        Uninstalling pip-9.0.1:
          Successfully uninstalled pip-9.0.1
    Successfully installed pip-18.1
    
    D:\home\python364x64>pip install flask
    Collecting flask
      Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
    Collecting click>=5.1 (from flask)
      Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
    Collecting Jinja2>=2.10 (from flask)
      Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
    Collecting Werkzeug>=0.14 (from flask)
      Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
    Collecting itsdangerous>=0.24 (from flask)
      Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
    Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask)
      Downloading https://files.pythonhosted.org/packages/9d/80/9a5daf3ed7b8482e72ee138cef602b538cfba5c507e24e39fb95c189b16b/MarkupSafe-1.1.0-cp36-cp36m-win_amd64.whl
    Installing collected packages: click, MarkupSafe, Jinja2, Werkzeug, itsdangerous, flask
      The script flask.exe is installed in 'D:\home\python364x64\Scripts' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Successfully installed Jinja2-2.10 MarkupSafe-1.1.0 Werkzeug-0.14.1 click-7.0 flask-1.0.2 itsdangerous-1.1.0
    

    1. Upload the app.py & web.config files,

    Here is their content as below.

    app.py Content:

    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return "Hello World!"
    

    web.config Content:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <appSettings>
            <add key="PYTHONPATH" value="D:\home\site\wwwroot" />
            <add key="PATH" value="D:\home\python364x64;D:\home\python364x64\Scripts;%PATH%" />
        </appSettings>
      <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
        </handlers>
        <httpPlatform processPath="D:\home\python364x64\Scripts\flask.exe" arguments="run --port %HTTP_PLATFORM_PORT%" stdoutLogEnabled="true" startupRetryCount='10'>
        </httpPlatform>
      </system.webServer>
    </configuration>
    

    Then fresh your browser or restart your API App first to browser, you will see as the figure below.

    这篇关于在Azure API上托管Flask(python)API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆