Hosting Flask(python) API on Azure API

ε祈祈猫儿з 提交于 2019-12-23 06:07:19

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/53906301/hosting-flaskpython-api-on-azure-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!