Forums

Python Flask [Errno 98] Address already in use

Hi,

I am trying to launch a flask app, but I have the following error message :

Traceback (most recent call last):
  File "/home/moibnj/flask_2017/flask_app.py", line 136, in <module>
    app.run(debug=True)
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 843, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python3.5/dist-packages/werkzeug/serving.py", line 677, in run_simple
    s.bind((hostname, port))
**OSError: [Errno 98] Address already in use**

Thank you for you help

Because you're using app.run. See the docs here and here

I used app.run with the recommended way :

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

but I heave the error mentioned above

Are you trying to run a Flask app in a console? Don't do that. It won't work. Our console servers are not able to server web applications. That's what we have the web apps tab for.

In fact on the web, I have a Something went wrong :-( page, that is why I am trying to debug on the console.

The error that is causing that page will be shown in your error log.

I am also having the same problem, but there are no errors listed in my error log. I get this same socket error when I run my app in the console, I am also using app.run in the recommended way, and I also get the "Something went wrong :-(" page. Did you ever figure out a solution?

You can't run your app in the console. That's what web apps are for. If you get a "Something went wrong" page, you'll either have an exception in your error log or in your server log (unless your app messes with logging in some way and prevents the message from getting to the log)

[edited by admin for formatting]

from flask import Flask
import MySQLdb
app = Flask(__name__)

@app.route('/')
def homepage():
    conn=MySQLdb.connect(host="********************************",
                         user="*********",
                         passwd="******",
                         db="*********")
    c=conn.cursor()
    return "hello"


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

this code gives this error :

Traceback (most recent call last):
  File "/home/hackathon/hostel/__init__.py", line 16, in <module>
    app.run()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 841, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/serving.py", line 708, in run_simple
    inner()
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/serving.py", line 670, in inner
    fd=fd)
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/serving.py", line 564, in make_server
    passthrough_errors, ssl_context, fd=fd)
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/serving.py", line 476, in __init__
    HTTPServer.__init__(self, (host, int(port)), handler)
  File "/usr/lib/python3.6/socketserver.py", line 453, in __init__
    self.server_bind()
  File "/usr/lib/python3.6/http/server.py", line 136, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib/python3.6/socketserver.py", line 467, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use

how to solve?

Hi there, these two guides should help?

http://help.pythonanywhere.com/pages/Flask

https://blog.pythonanywhere.com/121/

No , still getting same error.

You will generally get that error. Those pages where to explain what's happening and what you should do instead.

Okay..thanks

I also get the same Error but with a different situation

The error mentioned below:

2020-05-04 09:59:50,785: Error running WSGI application 2020-05-04 09:59:50,797: OSError: [Errno 98] Address already in use 2020-05-04 09:59:50,797: File "/var/www/amanojha_pythonanywhere_com_wsgi.py", line 16, in <module> 2020-05-04 09:59:50,797: from manage import application as app # noqa 2020-05-04 09:59:50,798: 2020-05-04 09:59:50,798: File "/home/amanojha/mysite/manage.py", line 157, in <module> 2020-05-04 09:59:50,798: application.run() 2020-05-04 09:59:50,798: 2020-05-04 09:59:50,798: File "/home/amanojha/.virtualenvs/flask/lib/python3.7/site-packages/flask/app.py", line 990, in run 2020-05-04 09:59:50,799: run_simple(host, port, self, **options) 2020-05-04 09:59:50,799: 2020-05-04 09:59:50,799: File "/home/amanojha/.virtualenvs/flask/lib/python3.7/site-packages/werkzeug/serving.py", line 1052, in run_simple 2020-05-04 09:59:50,800: inner() 2020-05-04 09:59:50,800: 2020-05-04 09:59:50,800: File "/home/amanojha/.virtualenvs/flask/lib/python3.7/site-packages/werkzeug/serving.py", line 1005, in inner 2020-05-04 09:59:50,800: fd=fd, 2020-05-04 09:59:50,800: 2020-05-04 09:59:50,801: File "/home/amanojha/.virtualenvs/flask/lib/python3.7/site-packages/werkzeug/serving.py", line 848, in make_server 2020-05-04 09:59:50,801: host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd 2020-05-04 09:59:50,801: 2020-05-04 09:59:50,801: File "/home/amanojha/.virtualenvs/flask/lib/python3.7/site-packages/werkzeug/serving.py", line 740, in init 2020-05-04 09:59:50,801: HTTPServer.init(self, server_address, handler) 2020-05-04 09:59:50,801: 2020-05-04 09:59:50,801: File "/usr/lib/python3.7/socketserver.py", line 452, in init 2020-05-04 09:59:50,802: self.server_bind() 2020-05-04 09:59:50,802: 2020-05-04 09:59:50,802: File "/usr/lib/python3.7/http/server.py", line 137, in server_bind 2020-05-04 09:59:50,802: socketserver.TCPServer.server_bind(self) 2020-05-04 09:59:50,802: 2020-05-04 09:59:50,803: File "/usr/lib/python3.7/socketserver.py", line 466, in server_bind 2020-05-04 09:59:50,803: self.socket.bind(self.server_address)

See our help page about not calling app.run in flask web apps: http://help.pythonanywhere.com/pages/Flask/

Why does ChatgGPT recommend this solution when it does not work ?

We cannot control what ChatGPT tells you.

These apps do not integrate and work and they have compability issues at many levels authentication TLS protocols and all sorts of things but they claim something different and have restrictions.

Could you provide more details? It's hard to tell what is your specific problem.