0

I have a very simple flask app that imports a py file in a different folder and returns output. Here is my flash app code:

from flask import Flask, request
app = Flask(__name__)

@app.route('/login', methods=['POST'])
def flask_test():
    # Imports login.py from python/libs folder
    from python.libs import login
    # input coming from a .php file 
    host = request.form.get('Distro','')
    #simple py file to test login and return pass or fail
    result = login.telnet_host(host)
    return result

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

I know code and flask works because if I comment out import line, and add some test code I get a correct response. I also created a test.py file on same folder and used same import command and running "python test.py" works fine as well. However for some reason, import statement in flask.py file is not working.

if I do " python flask_home.py" I get no errors and see this:

* Running on http://127.0.0.1:5000/

but when I go to web and add the host name in host filed and hit submit it gives me 500 server error. Again, If I comment out import line and add test code it works. I even moved file in same folder and modified import line to "import login" but it still wouldn't work.

Does anybody knows why import is breaking it? My flask.py file is in /var/www and my login.py file is in /var/www/python/libs. I can not move both in same folder unfortunately. Please advice.

Thanks

Neo
  • 47
  • 8
  • 1
    Run with `app.run(debug=True)` and report back with the actual error. – sberry Jan 29 '18 at 21:45
  • I do not see anything. I tried via web browser and also "python flask.py" It looks the same but has an additional line saying " * Restarting with reloader". Is there any place I need to go to check logs? – Neo Jan 29 '18 at 21:54

1 Answers1

1

In the same directory of flask_home.py, enter python shell and try to import from python.libs import login to see if you have syntax error in your login.py file.

Make sure you didn't forget to put __init__.py files in python/ and python/libs/ folders.

  • I have __init__.py in same folder, in python and also in login. Doing this returns nothing: >>> from python.libs import login >>> – Neo Jan 29 '18 at 21:49
  • Try to execute the code. And do as sberry told you: Run with `app.run(debug=True)` and post here the real error you have. – Thiago F. Pappacena Jan 29 '18 at 21:51
  • see my comment under sberry's comment – Neo Jan 29 '18 at 21:56
  • Do you have any exception when running `login.telnet_host(host)` from your shell? – Thiago F. Pappacena Jan 29 '18 at 22:07
  • Add some logging to see what you are receiving on request.form (example of logging setup here: https://gist.github.com/ibeex/3257877). Also, are you sure you are receiving this request as a POST? Check also request.args. (see this: https://stackoverflow.com/a/16664376/4271929) – Thiago F. Pappacena Jan 29 '18 at 22:35
  • Import is now working. Turns out Flask does not like print statement in py files...... It is still not working but Flask is more problematic than I thought. I has some limitation and some other action in my login code is probably breaking it. Painful .. – Neo Jan 30 '18 at 00:02