Forums

except Exception as e:#012OperationalError: (_mysql_exceptions.OperationalError) (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

I have created a simple flask app using mysql database and i am trying to deploy it. I have created a virtualenv and uploaded the code which looks something like this.

from flask import Flask,request,session
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 
'mysql://[username]:[password]@[username].mysql.pythonanywhere-services.com/[username]$[db]'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
app.secret_key = "any random string"

class Users(db.Model):
__tablename__='users'
email = db.Column('email',db.String,primary_key=True)
password = db.Column('pass',db.String)
name = db.Column('name',db.String)

def __init__(self,name=None,email=None,password=None):
    self.name = name
    self.email = email
    self.password = password

@app.route('/')
def index():
        return "Hello I am alive!"

@app.route('/login',methods=['GET','POST'])
 def login():
   if request.method == 'POST':
            if request.headers['Content-Type'] == 'application/json':
                email = request.args.get('email')
                passwd = request.args.get('pass')
                final = " "
                try:
             result = db.engine.execute("SELECT email,pass FROM users")
                except Exception as e:
                  raise e
                for row in result:
                if row[0] == email:
                  if row[1] == passwd:
                    session['email'] = email
                    final = "User Verified!"
                    break
                else:
                    final = "Password Incorrect!"
                    break
            else:
                final = "Please Register!"

         return final

    @app.route('/signup', methods=['POST','GET'])
    def signup():
     if request.method == 'POST':
         if request.headers['Content-Type'] == 'application/json':
             email = request.args.get('email')
             passwd = request.args.get('pass')
             name = request.args.get('name')
             try:
             newUser = Users(name,email,passwd)
            #print newUser
             except Exception as e:
               raise e
             db.session.add(newUser)
             db.session.commit()
   return "Successful!"

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

Also, when i run the command

   mysql -u username -h [username].mysql.pythonanywhere-services.com -p

It successfully logins the mysql database and I can access all my data. But when I am sending a request using the requests module of python using the following syntax

requests.post(url='http://nile2203.pythonanywhere.com/login',params=data,headers=headers)

but I am getting the above error in error log. Please help me find a solution because I have to use this server to access via a kivy android application. Thanks in advance.

Can I take a look at your code? We can see it from our admin interface, but we always ask for permission first.