Python Socket.Error: [Errno 98] Address Already In Use

12.7K    Asked by AlGerman in SQL Server , Asked on Nov 17, 2022

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 Amit jaisawal
  Regarding the oserror: [errno 98] address already in use 

This happens because you trying to run service at the same port and there is an already running application. it can happen because your service is not stopped in the process stack. you just have to kill those processes. There is no need to install anything here is the one line command to kill all running python processes.

for Linux based OS:
Bash:
kill -9 $(ps -A | grep python | awk '{print $1}')
Fish:
kill -9 (ps -A | grep python | awk '{print $1}')


Your Answer

Answer (1)

The error Python Socket.Error: [Errno 98] Address Already In Use occurs when a program tries to bind a socket to a port that is already in use by another process or hasn’t been properly released after a previous use. Here’s how you can resolve this issue:

Why It Happens:

  • A socket is already bound to the specified port and actively listening.
  • The operating system has not yet released the port after the program closed (TIME_WAIT state).
  • A duplicate process is trying to bind to the same port.

Steps to Resolve:

1. Find the Process Using the Port:

Use the netstat command to identify the process:

  netstat -tuln | grep 

Alternatively, on newer systems:

  ss -tuln | grep 

Identify the process ID (PID) from the output.

2. Kill the Process:

Terminate the process using the port:

  kill -9 

3. Enable Socket Reuse in Your Code:

Use the SO_REUSEADDR option in your Python socket code to allow reusing the address:

  import sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)s.bind(('0.0.0.0', ))s.listen(5)

4. Wait for the Port to Be Released:

  • If the process was recently stopped, the port might still be in the TIME_WAIT state. Waiting a few seconds may resolve the issue.

5. Avoid Binding to Fixed Ports:

Use port 0 to let the OS dynamically assign an available port:

  s.bind(('0.0.0.0', 0))

Best Practices:

  • Use SO_REUSEADDR to avoid conflicts in development environments.
  • Ensure only one instance of your application runs on a given port.

These steps will help you diagnose and fix the issue quickly!

3 Weeks

Interviews

Parent Categories