Python - Socket - Error: (Errno 48) Address Already in Use - Stack Overflow
Python - Socket - Error: (Errno 48) Address Already in Use - Stack Overflow
Products Search… 1 1
Home
socket.error: [Errno 48] Address already in use Ask Question
PUBLIC Asked 7 years, 7 months ago Active 8 days ago Viewed 433k times
Questions
I'm trying to set up a server with python from mac terminal.
Tags The Overflow Blog
What is Teams?
2 Kill the other process or run this one with a different port: python -m SimpleHTTPServer 8081 – Linked
Blender Sep 28 '13 at 20:50
-2 How to solve a Python Flask Socket Error?
Add a comment
81 How to find processes based on port and
kill them all?
12 Answers Active Oldest Votes
4 Shutting down Python BasicHTTPServer
from another thread
You already have a process bound to the default port (8000). If you already ran the same module 0 How can i change a service running on one
before, it is most likely that process still bound to the port. Try and locate the other process first: port number, now i want to run another
397 service on same old port number
Share Edit Follow edited Jun 28 '17 at 11:21 answered Sep 28 '13 at 20:54 Hot Network Questions
Martijn Pieters ♦
882k 241 3468 Can I trust a repo I am unfamiliar with to provide
me a secure PHP 8.0 package?
2975
Time complexity O(m+n) Vs O(n)
What url should I have in browser to see if it is working? I'm running on a different port as you sugested – Where is the source of the Seneca quote “to good,
irm Sep 28 '13 at 21:02 it is not enough to be better than the worst”?
14 might need to use sudo kill -9 PID – Danpe Jun 11 '15 at 22:11 What is the purpose of budgeting if all I need to do
is look at my bank/credit card statement?
Thanks, @Danpe, I tried "sudo kill PID" which didn't work but "sudo kill -9 PID" killed the process. Anyone
How do we know what the atmospheric pressure
know what -9 specifies? Even sudo manual doesn't seem to cover this parameter on Mars is?
sudo.ws/man/sudo.man.html – seokhoonlee Mar 17 '16 at 3:06
Multiple Wire.write() not working for Arduino Nano
3 @seokhoonlee: kill sends a signal to the process, which it can decide to handle (like shut down I2C
gracefully or rotate a logfile). These signals are integers (each with a name), the default being 15,
No canonical isomorphism
meaning TERM or terminate. Using -9 sends signal 9, KILL , which a process can't catch and ignore,
and the OS will end the process wether it likes to or not. – Martijn Pieters ♦ Mar 17 '16 at 10:51 Why does the B-29 have eight oil needles?
@seokhoonlee: also see the Unix signal article on Wikipedia. – Martijn Pieters ♦ Mar 17 '16 at 10:56 Interaction of \bm with optional argument
Show 1 more comment How and why can multiple people control the
Boeing B-29?
2. Kill the process on that port: MtG Arena - Dina and lifelink
10 This answer would benefit from an example of what the lsof output might look like, and how to find the Were kamikaze pilots an effective strategy for
process ID (the "XXXX" you list) within the output. For anyone seeing this without that information, it's the Japan?
second field in the output, under a header label of "PID". – lindes Feb 15 '18 at 20:15
Does a SQL Server Update statement for nvarchar
overwrite the same address on disk if the new
2 @lindes You are a crack! – Carlos Rodríguez Feb 20 '19 at 17:33
value is the same size? (Microsoft SQL Server)
@CarlosRodríguez: huh? I genuinely don't know what you mean by that comment (I know multiple Can I cut the steerer tube with a pipe cutter?
definitions of "crack", and the ones I know don't seem to fit...) – lindes Feb 21 '19 at 21:31
2 @CarlosRodríguez I want to believe he found your response absolutely on point. I think your observation Question feed
on the need to give samples of the output of lsof and how to identify PID is very important. The second
item in each row returned is usually the PID, it is always under the PID column of the output –
Kudehinbu Oluwaponle Apr 4 '19 at 14:39
Add a comment
Use
This will give you a list of processes using the port if any. Once the list of processes is given, use
the id on the PID column to terminate the process use
Share Edit Follow edited Dec 22 '18 at 13:25 answered May 5 '17 at 13:56
Community ♦ candy_man
1 1 461 5 11
1 That's perfect, especially those working on MAC OSX and didn't use SO_REUSEPORT instead of
SO_REUSEADDR – snr May 13 '19 at 20:32
Add a comment
Simple one line command to get rid of it, type below command in terminal,
19 ps -a
This will list out all process, checkout which is being used by Python and type bellow command in
terminal,
kill -9 (processID)
3 Working on MacOSX (i do not know if that is relevant), but I had to use the "-9" argument to make it work.
This is the only answer that mentions that. – Ideogram Sep 29 '20 at 13:24
Add a comment
By the way, to prevent this from happening in the first place, simply press Ctrl + C in terminal
while SimpleHTTPServer is still running normally. This will "properly" stop the server and release
17 the port so you don't have to find and kill the process again before restarting the server.
(Mods: I did try to put this comment on the best answer where it belongs, but I don't have enough
reputation.)
Share Edit Follow edited Oct 17 '16 at 5:39 answered Oct 14 '16 at 11:56
Andrew Mark Chapel
101 1 9 357 4 10
Useful info here, I was pressing Ctrl+Z unconciously and the process got kept alive. Ctrl+C releases the
port so no need to manually kill it. – BcK Dec 15 '20 at 14:42
Add a comment
You can also serve on the next-highest available port doing something like this in Python:
8 import SimpleHTTPServer
import SocketServer
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
port = 8000
while True:
try:
httpd = SocketServer.TCPServer(('', port), Handler)
print 'Serving on port', port
httpd.serve_forever()
except SocketServer.socket.error as exc:
if exc.args[0] != 48:
raise
print 'Port', port, 'already in use'
port += 1
else:
break
If you need to do the same thing for other utilities, it may be more convenient as a bash script:
#!/usr/bin/env bash
MIN_PORT=${1:-1025}
MAX_PORT=${2:-65535}
(netstat -atn | awk '{printf "%s\n%s\n", $4, $4}' | grep -oE '[0-9]*$'; seq "$MIN_PORT" "$MAX_PORT") | sort -R | head -n 1
Set that up as a executable with the name get-free-port and you can do something like this:
someprogram --port=$(get-free-port)
That's not as reliable as the native Python approach because the bash script doesn't capture the
port -- another process could grab the port before your process does (race condition) -- but still
may be useful enough when using a utility that doesn't have a try-try-again approach of its own.
Share Edit Follow edited Sep 2 '16 at 11:23 answered Sep 21 '15 at 14:31
Chris Johnson
17.3k 4 68 73
Add a comment
I am new to Python, but after my brief research I found out that this is typical of sockets being
binded. It just so happens that the socket is still being used and you may have to wait to use it. Or,
6 you can just add:
tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
This should make the port available within a shorter time. In my case, it made the port available
almost immediately.
Share Edit Follow edited Mar 26 '18 at 5:08 answered Mar 26 '18 at 4:01
Trinidad PoisonIvee
2,630 1 21 42 71 1 2
Add a comment
$ ps ax | grep python
$ kill PROCESS_NAME
Add a comment
5 Whether the server will allow the reuse of an address. This defaults to False , and can be
set in subclasses to change the policy.
Add a comment
I have a raspberry pi, and I am using python web server (using Flask). I have tried everything
above, the only solution is to close the terminal(shell) and open it again. Or restart the
3 raspberry pi, because nothing stops that webserver...
Add a comment
0 It is better to have it as function in your local system. (So better to keep this script in one of the
shell profile like ksh/zsh or bash profile based on the user preference)
function killport {
kill -9 `lsof -i tcp:"$1" | grep LISTEN | awk '{print $2}'`
}
Usage:
killport port_number
Example:
killport 8080
Add a comment
Adding to the answer from Michael Schmid Just had the problem, to allow rebinding of the port use
needs to SUBCLASS the socket server like this:
0
from socketserver import TCPServer, BaseRequestHandler
from typing import Tuple, Callable
class MySockServer(TCPServer):
def __init__(self, server_address: Tuple[str, int], RequestHandlerClass: Callable[..., BaseRequestHandler]):
self.allow_reuse_address = True
super().__init__(server_address, RequestHandlerClass)
because after instantiation, there is not point in changing that flag. Then use it instead of
TCPServer or whatever you are using.
Add a comment
Highly active question. Earn 10 reputation in order to answer this question. The reputation requirement helps
protect this question from spam and non-answer activity.
Not the answer you're looking for? Browse other questions tagged python macos simplehttpserver or
ask your own question.
STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE Blog Facebook Twitter LinkedIn Instagram
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Life / Arts
Developer Jobs Directory Advertising Work Here
Culture / Recreation
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Other
Mobile Terms of Service
Disable Responsiveness Contact Us
Cookie Settings
site design / logo © 2021 Stack Exchange Inc; user contributions
Cookie Policy licensed under cc by-sa. rev 2021.4.23.39140