def getBaseURL(req):
    """
    Given a Django web request object, returns the OpenID 'trust root'
    for that request; namely, the absolute URL to the site root which
    is serving the Django request.  The trust root will include the
    proper scheme and authority.  It will lack a port if the port is
    standard (80, 443).
    """
    name = req.META['HTTP_HOST']
    try:
        name = name[:name.index(':')]
    except:
        pass
    try:
        port = int(req.META['SERVER_PORT'])
    except:
        port = 80
    proto = req.META['SERVER_PROTOCOL']
    if 'HTTPS' in proto:
        proto = 'https'
    else:
        proto = 'http'
    if port in [80, 443] or not port:
        port = ''
    else:
        port = ':%s' % (port,)
    url = '%s://%s%s/' % (proto, name, port)
    return url
