The OsError: [errno 48] address already in use is very rare to find the error that causes when the current port is being used by a socket other than the default socket. There could be many reasons for this error, and this guide will give you a walkthrough for the oserror: [errno 48] address already in use and help you find the solutions to resolve the error.
- 1 What is OsError: [errno 48] address already in use?
- 2 Cause for the OsError: [errno 48] address already in use
- 3 Solution for the OsError: [errno 48] address already in use
- 4 FAQs
- 5 Conclusion
- 6 Reference
What is an Oserror?
The oserror of Python is a class of I/O errors that fall under exception errors. This error can be raised if the OS module is inaccessible or has an invalid file path.
What is OsError: [errno 48] address already in use?
The OsError: [errno 48] address already in use error arises when the current port is busy from being used by another socket that is not the default socket. This occurs when the default socket does not allow you to run the following process in the queue.
Cause for the OsError: [errno 48] address already in use
As mentioned above, the OsError: [errno 48] address already in use arises when the current port uses another socket other than the default socket. One of the main reasons for the current port to use a port other than the default port is having a busy default port. This may occur when the default port is already using a process and is not done with that. It could happen if the process is unfinished or is disconnected instead of terminating it.
The disconnection may have occurred when closing the server. While attempting to do so, the PyCharm will present you with two options: disconnect or terminate. So, instead of choosing the termination option, if you choose the disconnect option, the process will keep running in the background; this will make the address busy, so the default port won’t be able to work. Therefore, you will receive the OsError: [errno 48] address already in use.
Solution for the OsError: [errno 48] address already in use
The OsError: [errno 48] address already in use can be resolved using a different available port, forcibly reusing the busy address, or terminating the running processes in the background. To do so, there are ways mentioned below that can be used.
Using setsockopt() function
One of the ways to solve the OsError: [errno 48] address already in use is to use the setsockopt() function. This function can control the socket behavior. In the setsockopt() function, if you use the SO_REUSEADDR socket option, it can make the Python Socket forcibly bind to a busy port even if another socket is using it. This method is also known as the magical one line as it requires only a one-liner command to eliminate the OsError: [errno 48] address already in use error.
tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Terminating the already running process
This method requires identifying and stopping the already running process on the required port in the background. By doing so the OsError: [errno 48] address already in use can be resolved easily. The first step is to identify the process running on the required port. To do so, you can use the ps command in the way mentioned below in order to get the list of processes running in the background.
Apart from the “ps” command, you can also use the “lsof” or the “fuser” command. These commands can give you the technical information, whereas the “-f” command will provide you with the detailed format. The “grep python” command will help you filter out the processes you need information about.
After identifying the process, you need to eliminate the process. To do so, you need to run the kill command as follows.
Choosing another available port: –
This is a more convenient method as it does not keep you waiting for the default port and an easier method if the default port is a number the same or higher than 1024. But to execute this, you must first find an available port. To do so, you can use the following set of commands.
import SimpleHTTPServer import SocketServer Handler = SimpleHTTPServer.SimpleHTTPRequestHandler port = 8032 while True: try: httpd = SocketServer.TCPServer((”, port), Handler) print 'You are using,' port httpd.serve_forever() except SocketServer.socket.error as exc: if exc.args != 48: raise print 'Port', port, 'is busy' port += 1 else: break
This will help you find an available port. Once you find an available port, you need to use that, and to do so, you can follow the command mentioned below.
$ python -m SimpleHTTPServer <available port>
Here, you can use the port number you received from the previous command rather than the above one.
What is an I/O error?
The I/O errors are generally related to the input/output operation being failed. It is also an operating system-related error.
What is a socket?
It is a programming method to connect two nodes on a network in order to communicate with each other. Here, one node accepts the information on a particular port at an IP, and the other tries to form a connection.
This guide has all the information that can help you find the causes for OsError: [errno 48] address already in use and the probable solutions that can help you resolve the error without any issues.
To learn more about some common errors follow Python Clear’s errors section.