2

I'm trying to setup a server from Mac terminal.

I've established a connection on port 5000 and want to open another port in 5002 using this command:

python Blockchain.py -p 5002

But this gives the error:

OSError: [Errno 48] Address already in use

I tried to verify if port 5002 is open and listening. So I run shell command lsof -i:5002 but it returns none.

Stacktrace

Error log & stacktrace attached below:

Traceback (most recent call last):
 File "Blockchain.py", line 290, in <module>
    app.run(host='0.0.0.0', port=5000)
  File "/anaconda/envs/blockchain/lib/python3.6/site-packages/flask/app.py", line 841, in run
    run_simple(host, port, self, **options)
  File "/anaconda/envs/blockchain/lib/python3.6/site-packages/werkzeug/serving.py", line 814, in run_simple
    inner()
  File "/anaconda/envs/blockchain/lib/python3.6/site-packages/werkzeug/serving.py", line 774, in inner
    fd=fd)
  File "/anaconda/envs/blockchain/lib/python3.6/site-packages/werkzeug/serving.py", line 666, in make_server
    passthrough_errors, ssl_context, fd=fd)
  File "/anaconda/envs/blockchain/lib/python3.6/site-packages/werkzeug/serving.py", line 577, in __init__
    self.address_family), handler)
  File "/anaconda/envs/blockchain/lib/python3.6/socketserver.py", line 453, in __init__
    self.server_bind()
  File "/anaconda/envs/blockchain/lib/python3.6/http/server.py", line 136, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/anaconda/envs/blockchain/lib/python3.6/socketserver.py", line 467, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 48] Address already in use

Research

I've referred various answers on the same issue:

What I tried

Also, I tried the following methods to resolve the issue:

  1. Restarting the terminals
  2. Restarting PyCharm
  3. Running on different sockets (gives same error).

None of that seems to work. I'm fairly new to Python.

Any help is appreciated.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Prashant Mahajan
  • 175
  • 1
  • 1
  • 10

2 Answers2

3

While trying to implement answer suggested by DeepSpace I stumbled across the following answer:

Python - How to run multiple flask apps from same client machine

To run Flask application on different machines use:

export FLASK_APP=Blockchain.py
flask run --host 0.0.0.0 --port 5000

Next, open up another terminal and use:

export FLASK_APP=Blockchain.py
flask run --host 0.0.0.0 --port 5001

Credits: user metmirr

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Prashant Mahajan
  • 175
  • 1
  • 1
  • 10
0

If you look closely at the traceback you will see

app.run(host='0.0.0.0', port=5000)

which means that no matter what port is passed in by -p, port 5000 is hardcoded.

You will need to modify it to use whatever variable the script is storing the -p argument as.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154