'Returns the domain that should be set for the session cookie.
Uses ``SESSION_COOKIE_DOMAIN`` if it is configured, otherwise
falls back to detecting the domain based on ``SERVER_NAME``.
Once detected (or if not set at all), ``SESSION_COOKIE_DOMAIN`` is
updated to avoid re-running the logic.'

def get_cookie_domain(self, app):

    rv = app.config['SESSION_COOKIE_DOMAIN']
    if (rv is not None):
        return (rv if rv else None)
    rv = app.config['SERVER_NAME']
    if (not rv):
        app.config['SESSION_COOKIE_DOMAIN'] = False
        return None
    rv = rv.rsplit(':', 1)[0].lstrip('.')
    if ('.' not in rv):
        warnings.warn('"{rv}"    is    not    a    valid    cookie    domain,    it    must    contain    a    ".".    Add    an    entry    to    your    hosts    file,    for    example    "{rv}.localdomain",    and    use    that    instead.'.format(rv=rv))
        app.config['SESSION_COOKIE_DOMAIN'] = False
        return None
    ip = is_ip(rv)
    if ip:
        warnings.warn('The    session    cookie    domain    is    an    IP    address.    This    may    not    work    as    intended    in    some    browsers.    Add    an    entry    to    your    hosts    file,    for    example    "localhost.localdomain",    and    use    that    instead.')
    if ((self.get_cookie_path(app) == '/') and (not ip)):
        rv = ('.' + rv)
    app.config['SESSION_COOKIE_DOMAIN'] = rv
    return rv
