FIX: Python Socket Error 48: Address already in use
- Python is a very common programming language used in many of today's programs.
- The article below will show you how to handle Python Socket Error 48.
- If you can't handle Python, then maybe you should use a program to convert it into something you know.
- If you need more general troubleshooting guides, visit our Fix page.
You get the python socket error 48: Address already in use when a process attempts to bind itself to a busy port. Processes on the server connect to the internet via ports, and if you do not specify a port, the default port (8000) is used.
To solve this issue and clear the error, you have to bind the process to an unused port using one of the solutions in this guide.
How do I fix the python socket error 48: Address already in use?
1. Specify an unused port number for the process
- If you were creating the process using 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 should change the (Port Number) in the command to the actual port number.
- After running the above command, confirm if the python socket error 48 error is gone.
2. Free up the port
- Locate and list the processes using the port by running the command below:
$ ps -fA | grep python
- If successful, you will see the process codes in the argument, looking like the line below:
502 89332 12877 0 3:40PM ttys00 0:00.15 python -m SimpleHTTPServer
- From the argument above, if multiple python processes are active, it is easy to spot the process running SimpleHTTPServer.
- From the argument above, we can now kill the process with code 89332 to free up the port. Enter the following command to kill this process:
kill 89332
- If the process is not responding, you can also kill the process using the tougher command below:
sudo kill -9 PID
- The above command sends a standard SIGTERM signal.
- Finally, bind the process to the port you just freed up by running the following command:
$ python -m SimpleHTTPServer (Port Number)
NOTE that you should 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 proven to repair the python socket error 48.
3. Restart Raspberry Pi
Raspberry Pi cannot kill processes automatically, and so, the processes running on the ports must be ended manually.
This solution is basically the same principle as the above method. If you get the python socket error 48: Address already in use on Raspberry Pi, restarting it can fix the error.
As mentioned at the beginning of this troubleshooting guide, the process may already be bound to port 800 (the default port) if you ran it before.
You can easily clear the python socket error 48: Address already in use by specifying an unused port or freeing up the port that the process is bound to.
If you get the error on Raspberry Pi, simply restart it to repair.
By following any of the above-written methods you should be able to fix the Python Socket Error 48.
However, we would appreciate it if you let us know which method worked best for you by leaving us a message in the comments section below.
FAQ: Learn more about Python
- Can I use Python in a browser?
Yes, Python code can be created and edited in browser if you follow the right steps.
- Why did my Python console stop mid-coding?
If this happened to you, you were most likely the victim of a Python runtime error.
- My Python had problems loading? Why is that?
One cause for most Python issues is a missing or corrupt Python DLL.
Editor's Note: This article was originally published in February 2020 and was revamped and updated in July 2020 for freshness, accuracy, and comprehensiveness.