Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

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

Asked by Al German Nov 17, 2022 21.6K views 2 answers
Share

About this question

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

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jan 27, 2025

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!

Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.