Python - Socket Error, Address In Use

2019-07-21 04:54发布

I'm currently attempting to setup a SiriServer (that's beside the point) on Xubuntu 12.10 x64, when I run the server python returns error

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

The server by default is attempting to run on port 443, which unfortunetly is required in order for this application to work.

To double check if anything is running on port 443, I execute the following:

lsof -i :443

There's no results, unless I have something like Chrome or Firefox open, which I end up closing. Here's the full return from attempting to run the server application.

dustin@dustin-xubuntu:~/Applications/SiriServer$ sudo python siriServer.py
CRITICAL load_plugins Failed loading plugin due to missing module: 'Wordnik library not found. Please install wordnik library! e.g. sudo easy_install wordnik'
INFO <module> Starting Server
Traceback (most recent call last):
  File "siriServer.py", line 493, in <module>
    server = SiriServer('', options.port)
  File "siriServer.py", line 425, in __init__
    self.bind((host, port))
  File "/usr/lib/python2.7/asyncore.py", line 342, in bind
    return self.socket.bind(addr)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

I'm stuck on what to do, as this is the last part of setting up this application. Any help is appreciated.

4条回答
The star\"
2楼-- · 2019-07-21 05:09

This often happens when a python program doesn't exit properly when pressing ^C or ^Z. You could try reseting the terminal or exiting the terminal. You can also do killall -9 server.py

Another effective way to help prevent this even if you have root privs this can happen if a socket is not closed properly, here is a fix:

s=socket.socket( )
s.bind(("0.0.0.0", 8080))
while 1:
    try:
        c, addr = s.accept()
    except KeyBoardInterrupt:
        s.close()
        exit(0)
查看更多
贼婆χ
3楼-- · 2019-07-21 05:15

You're not root -- that's your problem. To bind to ports under 1024 on Unix, you must be the superuser. So, hit su and try the python code again. Alternatively, bind to a port from 1024 to 65535.

查看更多
Anthone
4楼-- · 2019-07-21 05:17

List all processes that you have running with

ps -a

Take the PID corresponding to python and pipe it into the kill command with (Example PID 2770)

kill -9 2770
查看更多
我想做一个坏孩纸
5楼-- · 2019-07-21 05:28

I got that error even if port number is more than 1024

You can use

    pkill -9 python

run command twice, it will list all python files which are killed

查看更多
登录 后发表回答