Python socket.error: [Errno 98] Address already in use

5.7K    Asked by ranjan_6399 in Python , Asked on Jun 30, 2021

when I set up application.py, it shows oserror: [errno 98] address already in use
Traceback (most recent call last): File "application.py", line 121, in main() File "application.py", line 117, in main http_server.listen(options.port) File "/usr/local/lib/python2.7/site-packages/tornado-3.1-py2.7.egg/tornado/tcpserver.py", line 117, in listen sockets = bind_sockets(port, address=address) File "/usr/local/lib/python2.7/site-packages/tornado-3.1-py2.7.egg/tornado/netutil.py", line 90, in bind_sockets sock.bind(sockaddr) File "/usr/local/ lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 98] Address already in use
Answered by Ranjana Admin

There is obviously another process of listening on the port. You might find out that process by using the following command:


    $ lsof -i :8000


or change your tornado app's port. tornado's error info not Explicitly on this.






Your Answer

Answer (1)

The Socket.Error: [Errno 98] Address Already In Use error in Python typically occurs when a socket is trying to bind to an address that is already in use by another socket. This can happen if a previous instance of your program did not close properly, or if another program is using the same port.

Here are some steps to resolve this issue:

Check for Running Processes: Ensure that no other process is using the same port. You can use tools like lsof or netstat to check which processes are using which ports.

  lsof -i :netstat -tuln | grep 

If you find a process using the port, you can terminate it or use a different port for your application.

Set the SO_REUSEADDR Socket Option: This option allows the socket to bind to an address that is in a TIME_WAIT state, which can help avoid the Address Already In Use error.

import socket

  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)s.bind((host, port))

Properly Close Sockets: Ensure your program properly closes sockets when it terminates. Use try-finally blocks to guarantee that sockets are closed.

import socket

  try:    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    s.bind((host, port))    s.listen(5)    # Your socket operations herefinally:    s.close()

Wait for the Socket to be Released: If the port is in use due to a previous process that has not yet fully terminated, waiting a few seconds can sometimes resolve the issue.

Restart Your System: If you are unable to identify and terminate the process using the port, restarting your system can free up the port.

Here is an example of using SO_REUSEADDR in a complete socket program:

  import sockethost = '127.0.0.1'port = 12345s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)try:    s.bind((host, port))    s.listen(5)    print(f"Server listening on {host}:{port}")    while True:        conn, addr = s.accept()        print(f"Connected by {addr}")        # Handle the connection        conn.close()finally:    s.close()

By following these steps, you should be able to resolve the Address Already In Use error and run your socket program successfully.








1 Month

Interviews

Parent Categories