Python [Errno 98] Address already in use - python

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?

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)

$ ps -fA | grep python
501 81211 12368 0 10:11PM ttys000 0:03.12
python -m SimpleHTTPServer
$ kill 81211

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}')

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
(via SocketServer.ThreadingTCPServer - Cannot bind to address after program restart )
This causes the init (constructor) to:
if self.allow_reuse_address:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

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!

A simple solution that worked for me is to close the Terminal and restart it.

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.

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

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

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

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.

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.

Got this error after I ran my code while programming a Pico W via Thonny. At the command line just do a socket.reset() to clear the issue.
>>> socket.reset()

The cleanest way to make the socket immediately reusable is to follow the recommendation to first shutdown the client end (socket) of a connection, and make sure the server's end shuts down last (through exception handling if needed).
This might well mean that the server end runs forever.
This is not a problem if that "forever" loop pauses execution, e.g. read from socket.
How you "break" that "forever" loop is up to you as server admin, as long as there are no clients (apart from obvious system level exceptions)

I tried the following code to settle the issue:
sudo lsof -t -i tcp:8000 | xargs kill -9

sudo pkill -9 python
try this command

Related

How to terminate current colab session from notebook cell

I'm trying to be a good citizen and make sure my notebook session is terminated immediately after running even if I'm not sitting at my machine.
Is there any code I can run in a notebook cell to achieve this?
A rather ugly but efficient solution might be
!kill $(ps aux | awk '{print $2}')
ps aux will give you a list of any process running in the machine, awk '{print $2}' will extract the PID of every process currently running and finally kill will stop them, sending a SIGTERM signal.
This will give you a message "Runtime disconnected" and the session will be closed, you can see it worked under "Runtime" > "Manage sessions".
You can see the available system aliases here
We have a way to do this correctly now:
from google.colab import runtime
runtime.unassign()
!pkill --oldest
!pkill is used to kill the program and --oldest is used to kill the oldest program
This might work give it a try.
!kill -9 -1

Checking for dead links locally in a static website (using wget?)

A very nice tool to check for dead links (e.g. links pointing to 404 errors) is wget --spider. However, I have a slightly different use-case where I generate a static website, and want to check for broken links before uploading. More precisely, I want to check both:
Relative links like file.pdf
Absolute links, most likely to external sites like example.
I tried wget --spyder --force-html -i file-to-check.html, which reads the local file, considers it as HTML and follows each links. Unfortunately, it can't deal with relative links within the local HTML file (errors out with Cannot resolve incomplete link some/file.pdf). I tried using file:// but wget does not support it.
Currently, I have a hack based on running a local webserver throught python3 http.serve and checking the local files through HTTP:
python3 -m http.server &
pid=$!
sleep .5
error=0
wget --spider -nd -nv -H -r -l 1 http://localhost:8000/index.html || error=$?
kill $pid
wait $pid
exit $error
I'm not really happy with this for several reasons:
I need this sleep .5 to wait for the webserver to be ready. Without it, the script fails, but I can't guarantee that 0.5 seconds will be enough. I'd prefer having a way to start the wget command when the server is ready.
Conversely, this kill $pid feels ugly.
Ideally, python3 -m http.server would have an option to run a command when the server is ready and would shutdown itself after the command is completed. That sounds doable by writing a bit of Python, but I was wondering whether a cleaner solution exists.
Did I miss anything? Is there a better solution? I'm mentioning wget in my question because it does almost what I want, but using wget is not a requirement for me (nor is python -m http.server). I just need to have something easy to run and automate on Linux.
So I think you are running in the right direction. I would use wget and python as they are two readily available options on many systems. And the good part is that it gets the job done for you. Now what you want is to listen for Serving HTTP on 0.0.0.0 from the stdout of that process.
So I would start the process using something like below
python3 -u -m http.server > ./myserver.log &
Note the -u I have used here for unbuffered output, this is really important
Now next is waiting for this text to appear in myserver.log
timeout 10 awk '/Serving HTTP on 0.0.0.0/{print; exit}' <(tail -f ./myserver.log)
So 10 seconds is your maximum wait time here. And rest is self-explanatory. Next about your kill $pid. I don't think it is a problem, but if you want it to be more like the way a user does it then I would change it to
kill -s SIGINT $pid
This will be equivalent to you processing CTRL+C after launching the program. Also I would handle the SIGINT my bash script as well using something like below
https://unix.stackexchange.com/questions/313644/execute-command-or-function-when-sigint-or-sigterm-is-send-to-the-parent-script/313648
The above basically adds below to top of the bash script to handle you killing the script using CTRL+C or external kill signal
#!/bin/bash
exit_script() {
echo "Printing something special!"
echo "Maybe executing other commands!"
trap - SIGINT SIGTERM # clear the trap
kill -- -$$ # Sends SIGTERM to child/sub processes
}
trap exit_script SIGINT SIGTERM
Tarun Lalwani's answer is correct, and following the advices given there one can write a clean and short shell script (relying on Python and awk). Another solution is to write the script completely in Python, giving a slightly more verbose but arguably cleaner script. The server can be launched in a thread, then the command to check the website is executed, and finally the server is shut down. We don't need to parse the textual output nor to send a signal to an external process anymore. The key parts of the script are therefore:
def start_server(port,
server_class=HTTPServer,
handler_class=SimpleHTTPRequestHandler):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
thread = threading.Thread(target=httpd.serve_forever)
thread.start()
return httpd
def main(cmd, port):
httpd = start_server(port)
status = subprocess.call(cmd)
httpd.shutdown()
sys.exit(status)
I wrote a slightly more advanced script (with a bit of command-line option parsing on top of this) and published it as: https://gitlab.com/moy/check-links

Stopping Django's Server [duplicate]

So im using ruby on rails in windows (i hear you all spitting your coffee onto the screen), its only a short term thing. (using ubuntu at home) So i tried to fire up webrick this afternoon and i get the error message
TCPServer Error, only one usage of each socket address is normally permitted
So it seems as if port 3000 is still running from last week? My question is how do i kill the process from the Windows command line. normally i have to press ctrl and pause/break in windows as ctrl c is not working which is only killing the batch process it seems..
Any solutions welcomed
Edit
So it seems as if
tasklist
will give me the list of processes, but where do i find the process for running the webrick server?
ruby.exe is not listed as a running process
Try using netstat -a -o -n to determine the pid of the process running on port 3000. Then you should be able to use taskkill /pid #### to kill whatever process is running on that port.
Probably not the most graceful way to do it, but I think it should work.
EDIT
You'll probably have to also use the /F flag to force-kill the process. I just tried it on my local machine, and that worked fine.
Go into rails_project\tmp\pids and delete the .pid file in there.
run:
rails server

Python in Raspberry socket.error: [Errno 98] Address already in use when creating socket [duplicate]

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?
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)
$ ps -fA | grep python
501 81211 12368 0 10:11PM ttys000 0:03.12
python -m SimpleHTTPServer
$ kill 81211
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}')
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
(via SocketServer.ThreadingTCPServer - Cannot bind to address after program restart )
This causes the init (constructor) to:
if self.allow_reuse_address:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
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!
A simple solution that worked for me is to close the Terminal and restart it.
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.
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
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
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
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.
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.
Got this error after I ran my code while programming a Pico W via Thonny. At the command line just do a socket.reset() to clear the issue.
>>> socket.reset()
The cleanest way to make the socket immediately reusable is to follow the recommendation to first shutdown the client end (socket) of a connection, and make sure the server's end shuts down last (through exception handling if needed).
This might well mean that the server end runs forever.
This is not a problem if that "forever" loop pauses execution, e.g. read from socket.
How you "break" that "forever" loop is up to you as server admin, as long as there are no clients (apart from obvious system level exceptions)
I tried the following code to settle the issue:
sudo lsof -t -i tcp:8000 | xargs kill -9
sudo pkill -9 python
try this command

How to kill all instance of uwsgi

I am trying to upload my project to the server. There is already a project in the server now. I have new project which I want to run and replace the old project with the new one, so I pull the new project to the server. Then I activate the virtual environment and do all the necessary work. Then when I try to run the command:
uwsgi --plugins=python --chdir=/var/www/prjt/src/ --socket=127.0.0.1:8889 --module=prjt.wsgi:application &
it tell me that
probably another instance of uWSGI is running on the same address (127.0.0.1:8889).
bind(): Address already in use [core/socket.c line 761]
I searched for similar problems and found some solutions about killing all instance of uwsgi as mentioned in this answer here but could not find how to do it.
for me the way to kill uwsgi instances in a bruteforce manner was:
sudo pkill -f uwsgi -9
Add a pidfile to your command:
uwsgi --plugins=python --chdir=/var/www/prjt/src/ --socket=127.0.0.1:8889 --module=prjt.wsgi:application --pidfile /tmp/myapp.pid
Then use
uwsgi --stop /tmp/myapp.pid
to stop the uwsgi instance in a safe way.
If you didn't specify a pidfile when you started the first instance, you can kill it brutally using
kill `pidof uwsgi`
you can get the pid of the uwsgi process here lsof -t -i tcp:8000
and kill it then kill -9 pid
I stop my uwsgi instance by command:
kill -INT `cat ${APP_ROOT}/run/uwsgi.pid`
This command sends signal to uwsgi which cause it to stop.
If you do not know PID than you may:
killall -s INT /ve/path/bin/uwsgi
The official documentation
Simliary, I had that issue too.
And I tried all the way(answer) that written in this page.
BUT Nothing was changed.
So I just restart the computer and the problem is gone!!!!!!!!!!!!
Try it!

Categories