简体   繁体   中英

Socket.error with foreman start: Tesing deploying a simple flask app to Heroku

The Problem

socket.error: [Errno 98] Address already in use

What I'm doing

I'm testing a simple flask app with foreman start in my local environment. The app contains a few functions but its erroring here:

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

The full trace is shown below. I'm not sure what could wrong, or how the socket could already be in use on my local machine. When I run the app with python runapp.py from the terminal everything works fine.

What I've tired.

Heroku, flask, and python sockets?

Deploying Flask app to Heroku

following that last question I tired to change the port it runs on by using

if __name__ == '__main__':    
    import os 
    port = int(os.environ.get('PORT', 5000)) 
    app.run(host='0.0.0.0', port=port)

But it didnt help. Any suggestions would be appreciated! I'm new to Heroku so please let me know if there is anymore information I can provided to make solving this easier. Thanks!

Stack trace of error:

        22:13:45 web.1  | started with pid 11121
        22:13:45 web.1  |  * Running on http://127.0.0.1:5000/
        22:13:45 web.1  | Traceback (most recent call last):
        22:13:45 web.1  |   File "Hunch/Hunch/hunchapp.py", line 49, in <module>
        22:13:45 web.1  |     app.run()
        22:13:45 web.1  |   File "/home/agconti/my_dev/github/Hunch/venv/local/lib/python2.7/site-packages/flask/app.py", line 772, in run
        22:13:45 web.1  |     run_simple(host, port, self, **options)
        22:13:45 web.1  |   File "/home/agconti/my_dev/github/Hunch/venv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 706, in run_simple
        22:13:45 web.1  |     test_socket.bind((hostname, port))
        22:13:45 web.1  |   File "/usr/lib/python2.7/socket.py", line 224, in meth
        22:13:45 web.1  |     return getattr(self._sock,name)(*args)
        22:13:45 web.1  | socket.error: [Errno 98] Address already in use
        22:13:45 web.1  | exited with code 1
        22:13:45 system | sending SIGTERM to all processes

Soultion:

from terminal:

heroku config:add PORT=33507

Then change your app to:

if __name__ == '__main__':
        import os  
        port = int(os.environ.get('PORT', 33507)) 
        app.run(host='0.0.0.0', port=port)

It seems that heroku reserves flask's default port for its own purposes. A detailed discussion can be found here: Deploying Flask app to Heroku .

When you terminate the flask application, use "Control + C." If you use "Control + Z" it will leave the remaining report activate for some reason... I don't know why, but this seems to be the case for me in Debian (wheezy).

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