-2

I am new to Python and Python-Flask and have ran into an error. I am using Ubuntu 18.04.3 LTS and Python 2.7.15+ to build a python-flask web application.

When I try to run the application I run into the following error:

  File "home_page.py", line 61, in <module>
    app.run(host="0.0.0.0", debug=True)
  File "/home/XXXXXXX/.local/lib/python2.7/site-packages/flask/app.py", line 990, in run
    run_simple(host, port, self, **options)
  File "/home/XXXXXXX/.local/lib/python2.7/site-packages/werkzeug/serving.py", line 988, in run_simple
    s.bind(server_address)
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

One of the fixes I have found was to close the window of puTTY (which I am using to access a VM) however, this doesnt seem the best way to solve this issue.

I am unsure how to proceed with this error. Any suggestions would be appreciated.

Rose
  • 71
  • 1
  • 2
  • 9
  • Can you please put whole `traceback`. To see which line of your code gives the error. – Hayat Nov 21 '19 at 14:03
  • 2
    Most likely some other application is already listening to the same port as you're trying to. This also can be a previously running instance of your app. – bereal Nov 21 '19 at 14:04
  • `netstat -lpn | grep :80 ` will give you the running process on port 80 (.i.e. `tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1607/apache2 `. Kill the process using process id (1607 for the above) `kill -9 1607`. Use `sudo` if required. – Arvind Nov 21 '19 at 14:10
  • Updated error to show full traceback error. – Rose Nov 21 '19 at 14:18
  • is this working correctly when you start Flask first time ? How do you run it? How do you stop it? Normally `Ctrl+C` should stop Flask and port should be available for next run. If you run it in background then it may still running when you try to run it again. – furas Nov 21 '19 at 14:23
  • @furas i type python filename.py to run the app. It works first time and to stop the app I use Ctrl+C. Then when I go to run the app again I get the error. – Rose Nov 21 '19 at 14:29
  • system sometimes blockes port for short time for some reason but only if socket wasn't closed correctly. But I never had this problem when I stop Flask using `Ctrl+C`. Besides in `debug` mode Flask should automaticaly reload code when you change some file and there is no need to restart it manually. I use Linux Mint based on Ubuntu. – furas Nov 21 '19 at 14:42

2 Answers2

1

Try this, should work for you.

    app.run(host="0.0.0.0", 
    port=5000,
    debug=True,
    threaded=True)

Putting port parameter with the value will enable your API to run on desired port.

Additional threaded parameter will help your API serve multiple request at a time.

Hope it helps.

Hayat
  • 1,539
  • 4
  • 18
  • 32
0

Specify your own port, don't use the reserved port 80. So you will change the following:

app.run("0.0.0.0:4000", debug=True)

Take a look at this as well regarding reserved ports: Reserved TCP/IP ports

ipinak
  • 5,739
  • 3
  • 23
  • 41
  • instead of [Errno 98] it changes to [Errno -2] once i change the port number to "0.0.0.0:4000" – Rose Nov 21 '19 at 14:25