Python [Errno 98] Address already in use
PythonSocketsConnectionErrnoPython Problem Overview
In my Python socket program, I sometimes need to interrupt it with Ctrl-C. When I do this, it does close the connection using socket.close()
.
However, when I try to reopen it I have to wait what seems like a minute before I can connect again. How does one correctly close a socket? Or is this intended?
Python Solutions
Solution 1 - Python
Yes, it is intended. Here you can read detailed explanation. It is possible to override this behavior by setting SO_REUSEADDR option on a socket. For example:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Solution 2 - Python
$ ps -fA | grep python
501 81211 12368 0 10:11PM ttys000 0:03.12
python -m SimpleHTTPServer
$ kill 81211
Solution 3 - Python
This happens because you trying to run service at the same port and there is an already running application. it can happen because your service is not stopped in the process stack. you just have to kill those processes.
There is no need to install anything here is the one line command to kill all running python processes.
for Linux based OS:
Bash:
kill -9 $(ps -A | grep python | awk '{print $1}')
Fish:
kill -9 (ps -A | grep python | awk '{print $1}')
Solution 4 - Python
If you use a TCPServer
, UDPServer
or their subclasses in the socketserver
module, you can set this class variable (before instantiating a server):
socketserver.TCPServer.allow_reuse_address = True
This causes the init (constructor) to:
if self.allow_reuse_address:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Solution 5 - Python
A simple solution that worked for me is to close the Terminal and restart it.
Solution 6 - Python
Nothing worked for me except running a subprocess with this command, before calling HTTPServer(('', 443), myHandler)
:
kill -9 $(lsof -ti tcp:443)
Of course this is only for linux-like OS!
Solution 7 - Python
First of all find the python process ID using this command
ps -fA | grep python
You will get a pid number by naming of your python process on second column
Then kill the process using this command
kill -9 pid
Solution 8 - Python
run the command
fuser -k (port_number_you_are _trying_to_access)/TCP
example for flask: fuser -k 5000/tcp
Also, remember this error arises when you interput by ctrl+z. so to terminate use ctrl+c
Solution 9 - Python
For Linux,
ps aux | grep python
This will show you the error. The process number (eg.35225) containing your python file is the error.
Now,
sudo kill -9 35225
This will kill the error process and your problem will be solved.
Solution 10 - Python
I faced similar error at odoo server and resolved that with these simple following steps:
-
Paste following code in terminal
ps -fA | grep python
You will get a pid number. Now copy the pid number from second column of terminal output.
-
Then write as below
kill -9 pid
The terminal will restart and then the command
flask run
Will work fine! Thank you
Solution 11 - Python
Do nothing just wait for a couple of minutes and it will get resolved. It happens due to the slow termination of some processes, and that's why it's not even showing in the running processes list.
Solution 12 - Python
I had the same problem (Err98 Address already in use) on a Raspberry Pi running python for a EV charging manager for a Tesla Wall Connector. The software had previously been fine but it stopped interrogating the solar inverter one day and I spent days thinking it was something I'd done in python. Turns out the root cause was the Wifi modem assigning a new dynamic IP to the solar inverter as as result of introducing a new smart TV into my home. I changed the python code to reflect the new IP address that I found from the wifi modem and bingo, the issue was fixed.
Solution 13 - Python
sudo pkill -9 python
try this command