Question

I am using Flask to build a mobile application. It worked fine before I set up Flask structure for large application. like this http://flask.pocoo.org/docs/patterns/packages/

WSGI worked fine when I had one python file. However, WSGI is not working after I import python files.

Here is directory structure.

/home
  /bridge
    index.py
    apis.wsgi
    /apis
      __init__.py
      settings.py

Index.py

from apis import app
app.run(host='0.0.0.0',debug=True)

apis.wsgi

import sys
sys.path.insert(0,'/home/bridge/apis')
from apis import app as application

init.py

from flask import Flask
app = Flask(__name__)

import apis.settings

settings.py

from flask import Flask
import os
import sys
from flask import render_template, request, jsonify, redirect, url_for, send_file, Response, make_response

@app.route('/apis/settings/aboutus')
def aboutus():
    return render_template('settings/aboutus.html')

When I run it after run #python index.py Domain.com/apis/settings/aboutus worked fine.

However, if I turn on WSGI it failed to load the page and showed this error.

Internal Server Error

Of course, I checked error log.

[error] [client 14.63.12.134] mod_wsgi (pid=2138): Exception occurred processing WSGI script '/home/bridge/index.wsgi'.
[error] [client 14.63.12.134] Traceback (most recent call last):
[error] [client 14.63.12.134]   File "/home/bridge/index.wsgi", line 3, in <module>
[error] [client 14.63.12.134]     from index import app as application
[error] [client 14.63.12.134]   File "/home/bridge/index.py", line 2, in <module>
[error] [client 14.63.12.134]     app.run(host='0.0.0.0',debug=True)
[error] [client 14.63.12.134]   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
[error] [client 14.63.12.134]     run_simple(host, port, self, **options)
[error] [client 14.63.12.134]   File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 706, in run_simple
[error] [client 14.63.12.134]     test_socket.bind((hostname, port))
[error] [client 14.63.12.134]   File "/usr/lib/python2.7/socket.py", line 224, in meth
[error] [client 14.63.12.134]     return getattr(self._sock,name)(*args)
[error] [client 14.63.12.134] error: [Errno 98] Address already in use

I get these errors.

Can you see what is the problem? Thank you.

Was it helpful?

Solution

correct index.py to:

from apis import app

if __name__ == "__main__":
    app.run(host='0.0.0.0',debug=True)

Because the wsgi server will call your app, so you don't need call app.run again, otherwise is raises the "Address already in use" error. So we use the if __name__ == "__main__": to wrap the app.run(...). And the flask docs also point out this trick.

OTHER TIPS

I simply solved this problem.

in index.py

from apis import app
#app.run(host='0.0.0.0',debug=True)

I deleted run command, and it worked. When WSGI mod try to run it, app.run command was running already.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top