Error while attempting to bind on address

Well, I’m back again. Maybe I really should say hello to everyone and introduce myself. I’ll do it later. Now let’s talk about the problems I encountered.

Actually, I want to restructure my own private project and let the whole project structure clearer and more standardized. But when I run the server, it will prompt the following error

Blockquote
OSError: [Errno 99] error while attempting to bind on address (‘xxx.xxx.xxx.xxx’, 8000): cannot assign requested address

After that, I tried to use the simple example of sanic documentation Get Started again, but this time no error was reported

Finally, I tried Gino’s sanic example again, and it reported the same Error

I guess this error is caused by the database connection, because the database connection operation was not carried out the second time.

I just set ssh pubkey yesterday because I want to use windows terminal to connect to the remote server.

I thought it was this problem, so I deleted ssh pubkey today, but the problem still exists. What can I do next to solve this problem? :dizzy_face: :dizzy_face: :dizzy_face:

:wave:

That error means that there already is something running on Port 8000. So, if you had no other servers running, it seems like one process was still running before you tried to run it again.

Just curious, are you running inside Docker or directly on your machine? How are you running/stopping the process?

@ahopkins Uh, I’m very sure that port is not already in use , Even if I change the port, the same Error will be reported. I also monitor the port through netstat, but no program occupies this port.

I tried to run the service on the same port, the Error reported is:

OSError: [Errno 98] error while attempting to bind on address (‘0.0.0.0’, 8000): address already in use

It’s different with I encountered:

OSError: [Errno 99] error while attempting to bind on address (‘0.0.0.0’, 8000): cannot assign requested address

I run it directly on my machine,
my Linux version is Ubuntu18.04,
my Python version is Python3.7,
my sanic version is 20.6.3.

Sorry, I read through that error message too quickly. I have seen it one too many times, I made an assumption.

How are you running your DB? Sanic?

I ran a simple postgres instance on docker:

docker run \
    -p 5432:5432 \
    -h 0.0.0.0 \
    -e POSTGRES_HOST_AUTH_METHOD=trust \
    -e POSTGRES_USER=gino \
    -e POSTGRES_PASSWORD=gino \
    -e POSTGRES_DB=gino \
    postgres:12-alpine

And then the GINO example app that you linked. No problems. I am assuming there must be a mismatch in your connection settings. If you post some snippets, maybe we can help debug.

OK, the first code is upload to here,
you need do something to initialization it :

cd ~/PersonalBlog
rm aerich.ini
rm migrations/ -rf
aerich init -t config.AERICH_CONFIG
aerich init-db

Then, the folder will add new file aerich.ini and new folder migrations,

python server.py

the test.py is redundant file, please ignore it.

The third code is same as GINO example, it’s only one file :

from sanic import Sanic
from sanic.exceptions import abort
from sanic.response import json
from gino.ext.sanic import Gino

app = Sanic("123")
app.config.DB_HOST = 'localhost'
app.config.DB_PORT = 5432
app.config.DB_USER = 'postgre'
app.config.DB_PASSWORD = 'root'
app.config.DB_DATABASE = 'test'
app.config.DB_ECHO = True

db = Gino()
db.init_app(app)


class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.BigInteger(), primary_key=True)
    nickname = db.Column(db.Unicode())

    def __repr__(self):
        return '{}<{}>'.format(self.nickname, self.id)


@app.route("/users/<user_id>")
async def get_user(request, user_id):
    if not user_id.isdigit():
        abort(400, 'invalid user id')
    user = await User.get_or_404(int(user_id))
    return json({'name': user.nickname})


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

I was able to run both test.py and the sample script in your post above. I could not get server.py to run because there seems to be a lot of missing dependencies.

Where to go from here? I would like to continue to help you, but I think you need to distill the problem down as much as possible to some piece of runnable code that I can easily replicate. Perhaps inside a docker container?

I am not very familiar with Docker, I will study how to use Docker in my spare time, so the progress may be a little slow, and I will send it out when I have done this.

No need to on my account.

Have you tried moving your DB connection stuff into a listener?

@app.listener('before_server_start')
def db_setup(app, loop):
   ...

try lsof -i:8000 to check out which process use port 8000 then use kill pid.

@ahopkins Yeah, I tried it, but it reported the same Error.
I copy the project to my another machine, It’s also reported the same Error
Now I’m sure this error is caused by my code, I will try edit my code on weekend and
I will report where my code changed after I solved the problem.

@ZinkLu Uh, I’m very sure that port is not already in use .

either

netstat -anp |grep 8000

or

losf -i :8000

thanks for your help :smile: :smile:

I was able to run test.py on both macos 10.15.5 python3.7.7 and docker ubuntu 18.04 python3.8 . Did you try another port?