简体   繁体   中英

Can't connect to ms sql server using pymssql flask

I have an ubuntu server running Apache and am trying to connect to an sql server on the same network.

When I run python in terminal and import pymssql and then connect , there are no problems connecting but when I put the same code in the init .py it stops running at the conn=pymssql.connect line. Any body have a clue on this?

__init__.py

from flask import Flask, url_for, render_template
import pymssql

app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, I love Digital Ocean!"

@app.route("/Reports")
def test(): 
    conn=pymssql.connect(server='<fillinServer>', user='<fillinuser>', password='<fillinpassword>', database='<fillindatabase>')
    print "1"       
    cursor=conn.cursor()
    cursor.execute("SELECT TOP 1 * FROM testquery;")
    print "2"
    row=cursor.fetchone()
    print "3"
    t= row[1]
    return render_template("test.html", test=t) 

if __name__ == "__main__":
    app.run(debug=True)

.WSGI 



 #!/usr/bin/python
import sys
import logging

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/Reports/")

from ReportApp import app as application
application.secret_key = 'Add your secret key'

Try running your code directly from flask.

Change:

if __name__ == "__main__":
    app.run(
    host="0.0.0.0",     #replace this with your ip
    port=int("80"),     #replace with your port
    debug=True)

Make sure all packages and modules are correctly installed in root/virtual environment.

sudo python app.py

If you have tabs and 4 white spaces mixed it will alert you. Clean/debug your script and re-run it.

After fixing these if the script is running in web-browser, it is probably executing your command. My script took a while since I was connecting to a busy BI server.

ps add:

print("done")

so you know script has executed properly.

I can tell you that it worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM