'Gracefully shutdown a server that is serving forever.'

def stop(self):

    self.ready = False
    sock = getattr(self, 'socket', None)
    if sock:
        if (not isinstance(self.bind_addr, basestring)):
            try:
                (host, port) = sock.getsockname()[:2]
            except socket.error as x:
                if (x.args[0] not in socket_errors_to_ignore):
                    raise
            else:
                for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
                    (af, socktype, proto, canonname, sa) = res
                    s = None
                    try:
                        s = socket.socket(af, socktype, proto)
                        s.settimeout(1.0)
                        s.connect((host, port))
                        s.close()
                    except socket.error:
                        if s:
                            s.close()
        if hasattr(sock, 'close'):
            sock.close()
        self.socket = None
    self.requests.stop(self.shutdown_timeout)
