FIX: Python Socket Error 48: Address already in use

You get python socket error 48: address is already in use  when a process tries to bind to a busy port. Processes on the server connect to the Internet through ports, and if you do not specify a port, the default port (8000) is used.

To resolve this problem and clear the error, you must bind the process to an unused port using one of the solutions in this guide.

Contents index

  • How do I fix python socket error 48: address is already in use?
    • Specify an unused port number for the process
    • Free the port
    • Restart Raspberry Pi

How do I fix python socket error 48: address is already in use?

1. Specify an unused port number for the process

  1. If you were creating the process with the following command:

$ python -m SimpleHTTPServer

Add the port number after the above command, so that it becomes:

$ python -m SimpleHTTPServer (Port number)

NOTE that you must change the (Port Number) in the command to the actual port number.

  1. After running the above command, confirm if the Python socket error 48 is gone.

2. Free the port

  1. Locate and list the processes using the port by running the following command:

$ ps -fA | grep python

  1. If successful, you will see the process codes in the argument, like the line below:

502 89332 12877 0 3:40 PM ttys00 0: 00.15 python -m SimpleHTTPServer

  1. Based on the above argument, if there are multiple Python processes active, it is easy to detect the process running SimpleHTTPServer.
  2. From the above argument, we can now kill the process with code 89332 to free the port. Enter the following command to kill this process:

kill 89332

  1. If the process is unresponsive, you can also kill the process using the more difficult command below:

sudo kill -9 PID

  1. The above command sends a standard SIGTERM signal.
  2. Finally, bind the process to the port you just freed by running the following command:

$ python -m SimpleHTTPServer (Port number)

NOTE that you must change the (Port Number) in the command to the actual port number.

After entering the last command above, the process will be created on the free port. This method has tried to fix python socket error 48.

3. Restart Raspberry Pi

 

However, we would appreciate if you would let us know which method worked best for you by leaving us a message in the comment section below.

Leave a Comment

Scroll to Top