Getting application errors on Azure I dont get locally - python

I am developing a Flask/MongoDB application Im deploying on Azure. Locally, I am in the process of creating my models and testing my database connection. I am using Flask-MongoEngine to manage my DB connection. This is a sample of code that works perfectly on localhost but fails when calling its deployed version on Azure.
# On models.py
from flask_mongoengine import MongoClient
db = MongoClient()
class User(db.Document):
name = db.StringField(max_length=50)
token = db.StringField(max_length=50)
email = db.EmailField()
Later, from views.py I call my User class like this:
import models as mdl
#app.route('/test')
def test():
"""For testing purposes"""
user = mdl.User(name='Matias')
user.save()
users = mdl.User.objects
return jsonify(users)
which ouputs as expected locally. On Azure, however, I get the following error
(will only show the last and relevant part of the traceback):
File ".\app\views.py", line 53, in test
user = mdl.User(name='Matias')
File "D:\home\python364x86\lib\site-packages\mongoengine\base\document.py",
line 43, in _init_
self._initialised = False
File "D:\home\python364x86\lib\site-packages\mongoengine\base\document.py",
line 168, in _setattr_
self._is_document and
AttributeError: 'User' object has no attribute '_is_document'
Through pip freeze I checked I am using the same versions for mongoengine, pymongo and flask_mongoengine in both environments. I can't seem to find someone else with the same problem. The app is deployed as a webapp on a Windows machine in the Azure cloud.
Any help is appreciated, thanks.
PS: Further info
Reviewing mongoengine code, I found out that _is_document attribute is set inside a metaclass for the class Document (DocumentMetaclass and TopLevelDocumentMetaclass). I tried setting the attribute to True inside User, and the following error showed:
AttributeError: 'User' object has no attribute '_meta',
which is also an attribute defined inside those metaclasses. Somehow the metaclasses code is not running? Maybe the Azure environment has something to do with it?

Related

Discord.py | Mysqlx not working in heroku

Im trying to connect my DB for my discord bot.
I decided to use mysqlx module for python but when I run the code in heroku raises an error.
I'm using github for deployment method
Version checked.
It is locally working.
Checked get_session definition in local.
get_session definition in local:
def get_session(*args, **kwargs):
"""Creates a Session instance using the provided connection data.
Args:
*args: Variable length argument list with the connection data used
to connect to a MySQL server. It can be a dictionary or a
connection string.
**kwargs: Arbitrary keyword arguments with connection data used to
connect to the database.
Returns:
mysqlx.Session: Session object.
"""
settings = _get_connection_settings(*args, **kwargs)
return Session(settings)
I have another app hosted in Heroku that uses the same module and deployment method and works perfectly. I can't understand why it doesn't work in this heroku.
Heroku error:
File "/app/bot.py", line 25, in <module>
session = mysqlx.get_session({
AttributeError: module 'mysqlx' has no attribute 'get_session'
The code:
import mysqlx
session = mysqlx.get_session({
"host": "",
"port": 3306,
"user": "",
"password": ""
})

How to refer Environment variables mentioned in openshift deployment, inside python code running in the pod of same deployment

I am new to Openshift container platform. I am trying to deploy my ML model written in python, which serves the Json request based on authentication of name/password already mentioned in the py code.
#app.before_request
def validate_request():
try :
if(request.authorization.username != auth_user or request.authorization.password != auth_password):
return "Authentication failed: invalid or missing user/password."
except:
return "Authentication failed: invalid or missing user/password"
Now what I am doing is, I am creating a secrert dm in Openshift name-space and adding it in the deployment as below
How can I use the username and password mentioned in this secret in my python code, I have tried with the openshift docbut it doesnt complete my requirement of using the value in the code.
auth_user = os.environ.get('principle')
auth_password = os.environ.get('credential')

500 Internal Server Error when trying to connect through Postman Using PyCharm

Hi I am trying to test my connection to my database on my local drive for a personal project I am working on. I am trying to get better with python and MongoDB. Every time I try to test my connection using postman I am getting a 500 internal error. I double checked my URI = mongodb://127.0.0.1:27017 like majority local. I even uninstalled and reinstalled mongoDB. Any advice or solutions would be highly helpful. In my PyCharm IDE I have my database file
import pymongo
__author__ = 'jslvtr'
class Database(object):
URI = "mongodb://127.0.0.1:27017"
DATABASE = None
#staticmethod
def initialize():
client = pymongo.MongoClient(Database.URI)
Database.DATABASE = client['fullstack']
#staticmethod
def insert(collection, data):
Database.DATABASE[collection].insert(data)
#staticmethod
def find(collection, query):
return Database.DATABASE[collection].find(query)
#staticmethod
def find_one(collection, query):
return Database.DATABASE[collection].find_one(query)
Here is my app file:
from flask import Flask
from src.common.database import Database
__author__ = 'jslvtr'
app = Flask(__name__)
app.config.from_object('config')
app.secret_key ='123'
#app.before_first_request
def init_db():
Database.initialize()
from src.models.users.views import user_blueprint
app.register_blueprint(user_blueprint, url_prefix="/users")
Here is my config file:
__author__ = 'jslvtr'
DEBUG = True
ADMINS = frozenset([
"christopher.jxxxx#gmail.com"
])
This is the error I keep receiving:
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"
500 Internal Server Error
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
You must check mongodb from command line first like below
C:\Program Files\MongoDB\Server\3.6\bin>mongo localhost:27017 ;
if it works then you can jump into the code;
the issue you are getting error 500 is reserved for server only, meaning it doesn't link to your database connectivity at all. The request you are sending is wrong as per the REST endpoint you defined. Please verify the request with your code. If the request matches with your code you will get connection timeout error from MongoDB if you cannot connect to mongodb from localhost.
I think once you are okay with request you will be able to get a success.
You can paste the request JSON for more help.

internal error when connecting to google cloud SQL

I designed a simple website using Flask and my goal was to deploy it on Google App engine. I started working on it locally and used google cloud sql for the database. I used google_cloud_proxy to open the port 3306 to interact with my GC SQL instance and it works fine locally... this is the way I'm connecting my application to GC SQL:
I have a app.yaml file which I've defined my Global Variables in it:
env_variables:
CLOUDSQL_SERVER = '127.0.0.1'
CLOUDSQL_CONNECTION_NAME = "myProjectName:us-central1:project"`
CLOUDSQL_USER = "user"
CLOUDSQL_PASSWORD = "myPassword"
CLOUDSQL_PORT = 3306
CLOUDSQL_DATABASE = "database"
and from my local machine I do:
db = MySQLdb.connect(CLOUDSQL_SERVER,CLOUDSQL_USER,CLOUDSQL_PASSWORD,CLOUDSQL_DATABASE,CLOUDSQL_PORT)
and if I want to get connected on App Engine, I do:
cloudsql_unix_socket = os.path.join('/cloudsql', CLOUDSQL_CONNECTION_NAME)
db = MySQLdb.connect(unix_socket=cloudsql_unix_socket,user=CLOUDSQL_USER,passwd=CLOUDSQL_PASSWORD,db=CLOUDSQL_DATABASE)
the static part of the website is running but for example, when I want to login with a username and password which is stored in GC SQL, I receive an internal error.
I tried another way... I started a compute engine, defined my global variables in config.py, installed flask, mysqldb and everything needed to start my application. I also used cloud_sql_proxy on that compute engine and I tried this syntax to connect to GC SQL instance:
db = MySQLdb.connect(CLOUDSQL_SERVER,CLOUDSQL_USER,CLOUDSQL_PASSWORD,CLOUDSQL_DATABASE,CLOUDSQL_PORT)
but it had the same problem. I don't think that it's the permission issue as I defined my compute engine's ip address in the authorized network part of GC SQL and in I AM & ADMIN part, the myprojectname#appspot.gserviceaccount.com has the Editor role!
can anyone help me out where the problem is?
ALright! I solved the problem. I followed the Google cloud's documentation but I had problems.I added a simple '/' in:
cloudsql_unix_socket = os.path.join('/cloudsql', CLOUDSQL_CONNECTION_NAME)
instead of '/cloudsql' it should be '/cloudsql/'
I know it's weird because os.path.join must add '/' to the path but for strange reasons which I don't know, it wasn't doing so.

Google Cloud Datastore API in Python Code

I am trying to Implement Google Cloud DataStore in my Python Django Project not running on Google App Engine.
Can it be possible to use Google Datastore without having the project run on Google App Engine ? If yes, Can you please tell how to retrieve the complete entity object or execute the query successfully ?
The below code snippet prints the query object but throws an error after that.
Code Snippet:
from gcloud import datastore
entity_kind = 'EntityKind'
numeric_id = 1234
client = datastore.Client()
key = client.key(entity_kind, numeric_id)
query = client.query(kind=entity_kind)
print(query)
results = list(query.fetch())
print(results)
Error:
NotFound: 404 The project gproj does not exist or it does not contain an active App Engine application. Please visit http://console.developers.google.com to create a project or https://console.developers.google.com/appengine?project=gproj to add an App Engine application. Note that the app must not be disabled.
This guide will probably be helpful. You can see an example of it in action here.
You just need to pass a project id to the .Client() method:
datastore.Client("YOUR_PROJECT_ID")
You can also skip this part by running this command before running your app:
$ gcloud beta auth application-default login
If you run that, it will authenticate all of your requests locally without injecting the project id :)
Hope this helps!

Categories