The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Melt

--

Chapters
00:00 Question
00:33 Accepted answer (Score 451)
02:04 Answer 2 (Score 270)
02:27 Answer 3 (Score 53)
02:51 Answer 4 (Score 31)
03:20 Thank you

--

Full question
https://stackoverflow.com/questions/1907...

Accepted answer links:
[Wikipedia for more details]: https://en.wikipedia.org/wiki/Unix_signa...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #macos #simplehttpserver

#avk47



ACCEPTED ANSWER

Score 471


You already have a process bound to the default port (8000). If you already ran the same module before, it is most likely that process still bound to the port. Try and locate the other process first:

$ ps -fA | grep python
  501 81651 12648   0  9:53PM ttys000    0:00.16 python -m SimpleHTTPServer

The command arguments are included, so you can spot the one running SimpleHTTPServer if more than one python process is active. You may want to test if http://localhost:8000/ still shows a directory listing for local files.

The second number is the process number; stop the server by sending it a signal:

kill 81651

This sends a standard SIGTERM signal; if the process is unresponsive you may have to resort to tougher methods like sending a SIGKILL (kill -s KILL <pid> or kill -9 <pid>) signal instead. See Wikipedia for more details.

Alternatively, run the server on a different port, by specifying the alternative port on the command line:

$ python -m SimpleHTTPServer 8910
Serving HTTP on 0.0.0.0 port 8910 ...

then access the server as http://localhost:8910; where 8910 can be any number from 1024 and up, provided the port is not already taken.




ANSWER 2

Score 315


Simple solution:

  1. Find the process using port 8080:
sudo lsof -i:8080
  1. Kill the process on that port:
kill $PID

kill -9 $PID  //to forcefully kill the port

PID is got from step 1's output.




ANSWER 3

Score 57


Use

 sudo lsof -i:5000

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

 kill 379 #use the provided PID



ANSWER 4

Score 25


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 the port so you don't have to find and kill the process again before restarting the server.