import logging
import socket
import threading

from connection import Connection
import shared


class Listener(threading.Thread):
    def __init__(self, host, port, family=socket.AF_INET):
        super().__init__(name='Listener')
        self.host = host
        self.port = port
        self.family = family
        self.s = socket.socket(self.family, socket.SOCK_STREAM)
        self.s.bind((self.host, self.port))

    def run(self):
        self.s.listen(1)
        self.s.settimeout(1)
        while True:
            try:
                conn, addr = self.s.accept()
                logging.info('Incoming connection from: {}:{}'.format(addr[0], addr[1]))
                with shared.connections_lock:
                    c = Connection(addr[0], addr[1], conn)
                    c.start()
                    shared.connections.add(c)
            except socket.timeout:
                pass

import logging
import socket
import threading

from connection import Connection
import shared


class Listener(threading.Thread):
    def __init__(self, host, port, family=socket.AF_INET):
        super().__init__(name='Listener')
        self.host = host
        self.port = port
        self.family = family
        self.s = socket.socket(self.family, socket.SOCK_STREAM)
        self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.s.bind((self.host, self.port))

    def run(self):
        self.s.listen(1)
        self.s.settimeout(1)
        while True:
            try:
                conn, addr = self.s.accept()
                logging.info('Incoming connection from: {}:{}'.format(addr[0], addr[1]))
                with shared.connections_lock:
                    c = Connection(addr[0], addr[1], conn)
                    c.start()
                    shared.connections.add(c)
            except socket.timeout:
                pass

Add SO_REUSEADDR to socket options
Add SO_REUSEADDR to socket options

  import logging
  import socket
  import threading
  
  from connection import Connection
  import shared
  
  
  class Listener(threading.Thread):
      def __init__(self, host, port, family=socket.AF_INET):
          super().__init__(name='Listener')
          self.host = host
          self.port = port
          self.family = family
          self.s = socket.socket(self.family, socket.SOCK_STREAM)
+         self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
          self.s.bind((self.host, self.port))
  
      def run(self):
          self.s.listen(1)
          self.s.settimeout(1)
          while True:
              try:
                  conn, addr = self.s.accept()
                  logging.info('Incoming connection from: {}:{}'.format(addr[0], addr[1]))
                  with shared.connections_lock:
                      c = Connection(addr[0], addr[1], conn)
                      c.start()
                      shared.connections.add(c)
              except socket.timeout:
                  pass
  
Add SO_REUSEADDR to socket options
## Code Before:
import logging
import socket
import threading

from connection import Connection
import shared


class Listener(threading.Thread):
    def __init__(self, host, port, family=socket.AF_INET):
        super().__init__(name='Listener')
        self.host = host
        self.port = port
        self.family = family
        self.s = socket.socket(self.family, socket.SOCK_STREAM)
        self.s.bind((self.host, self.port))

    def run(self):
        self.s.listen(1)
        self.s.settimeout(1)
        while True:
            try:
                conn, addr = self.s.accept()
                logging.info('Incoming connection from: {}:{}'.format(addr[0], addr[1]))
                with shared.connections_lock:
                    c = Connection(addr[0], addr[1], conn)
                    c.start()
                    shared.connections.add(c)
            except socket.timeout:
                pass

## Instruction:
Add SO_REUSEADDR to socket options
## Code After:
import logging
import socket
import threading

from connection import Connection
import shared


class Listener(threading.Thread):
    def __init__(self, host, port, family=socket.AF_INET):
        super().__init__(name='Listener')
        self.host = host
        self.port = port
        self.family = family
        self.s = socket.socket(self.family, socket.SOCK_STREAM)
        self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.s.bind((self.host, self.port))

    def run(self):
        self.s.listen(1)
        self.s.settimeout(1)
        while True:
            try:
                conn, addr = self.s.accept()
                logging.info('Incoming connection from: {}:{}'.format(addr[0], addr[1]))
                with shared.connections_lock:
                    c = Connection(addr[0], addr[1], conn)
                    c.start()
                    shared.connections.add(c)
            except socket.timeout:
                pass
