1

I have created Cosmos DB account with MongoDB driver and want to access it from flask server. Here is the simplest example I'm trying:

from flask import Flask
from pymongo import MongoClient


url = 'monbodb://<my_db_name>.documents.azure.com:10255/?ssl=true
username = '<my_db_name>'
password = '<my_password>'
client = MongoClient(url, username=username, password=password)
app = Flask(__name__)


@app.route('/ping', methods=['GET'])
def ping():
    return 'pong!'


if __name__ == '__main__':
    app.run()

I deploy it with git and at the end it says the deployment was successful. But really app has crashed because webpage is not accessible, saying "The page cannot be displayed because an internal server error has occurred.". I guess issue is with SSL, because removing '/?ssl=true' does allow access app webpage but it this case DB is not accessible! What is with issue and how can it be fixed?

Rail Suleymanov
  • 361
  • 1
  • 4
  • 16

1 Answers1

1

I tried to access my mongo db in my azure flask web app and it works well for me. You could refer to my working steps and codes.

view.py

from datetime import datetime
from flask import render_template
from jaygongflask import app
import pymongo

@app.route('/mongo')
def mongo():
    uri = "mongodb://<account name>:<your account key>@<account name>.documents.azure.com:10255/?ssl=true&replicaSet=globaldb"
    client = pymongo.MongoClient(uri)
    db = client['db']
    coll = db['coll']
    doc= coll.find_one()
    return render_template(
        'mongo.html',
        title='Mongo',
        message='Mongo query result.',
        queryResult = doc
    )

Publish your flask project and add the web.config.

enter image description here

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="WSGI_HANDLER" value="<Your Project Name>.app"/>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
  </appSettings>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\Python361x64\python.exe|D:\home\Python361x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
  </system.webServer>
</configuration>

Switch to the Kudu CMD and commands cd Python361x64 and touch get-pip.py and copy the content of the url https://bootstrap.pypa.io/get-pip.py into the get-pip.py via Edit button, then run python get-pip.py to install the pip tool.

enter image description here

Here, I use python361x64 extension. So I run the command python -m pip install pyodbc in KUDU.

enter image description here

Get query result

Access the url http://***.azurewebsites.net/mongo .

enter image description here

More deployment details , please refer to this tutorial.

Hope it helps you.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • Hello, Jay. Thank you for your replay, I'm trying your solution. Could you please tell me which one of *.config files should I edit? Currently I see few possiblities: 1) web.2.7.config in "/d/home/site/repository", 2) web.2.7.config in "/d/home/wwwroot" and 3) web.config in "/d/home/site/wwwroot". – Rail Suleymanov Feb 21 '18 at 15:50
  • @RailSuleymanov You need edit the one which is stayed with your project(may be the No.3 /d/home/site/wwwroot you mentioned). please see my additional screenshot. – Jay Gong Feb 22 '18 at 01:12
  • apparently we’re in different time zones, because I didn’t have chance to check yet. I’ll let you as soon as I have one to try. But at the time of my yesterday reply I wasn’t able nor to create files (touch get-pip.py) nor to install packages in Kudu CMD, so I guess it’s gonna be challenging... – Rail Suleymanov Feb 22 '18 at 07:15
  • @RailSuleymanov Never mind. You could refer to the details from my previous case: https://stackoverflow.com/questions/47155223/install-odbc-driver-to-azure-app-service/47197435#47197435 . Any concern or you stuck with one step,please let me know. – Jay Gong Feb 22 '18 at 07:19
  • @RailSuleymanov Anyway, you could refer to the cases I answered previously, I believe you could make it out! https://stackoverflow.com/search?q=user%3A8198946+touch+get-pip.py – Jay Gong Feb 22 '18 at 07:22
  • Hi, I used python27 extension and could finally create get-pip.py and install pymongo along with adding web.config... What do I do next? How should I restart the app? I guess, these settings are different from virtual environment created by Azure so should start be different?.. – Rail Suleymanov Feb 23 '18 at 14:54
  • Well, I could launch the app with command "python ../site/wwwroot/main.py" and now it runs. I'm not sure if this is a legitimate approach, but anyway, thank you very much! – Rail Suleymanov Feb 23 '18 at 14:59