check whether the given cookie is sent to a secure host issecureprotocol super returnoksecurecookie request if issecureprotocol return true the original implementation of this method only takes secure protocols e g https into account but the latest developments in modern browsers chrome firefox assume localhost is also a secure location so we override it with our own strategy return self islocalhostcookiejar requesthostrequest def islocalhostself hostname implements the static localhost detection algorithm in firefox https searchfox orgmozillacentralrevd4d7611ee4dd0003b492b865bc5988a4e6afc985netwerkdnsdns cpp205218 return hostname localhost or hostname endswithlocalhostsuffix check whether the given cookie is sent to a secure host the original implementation of this method only takes secure protocols e g https into account but the latest developments in modern browsers chrome firefox assume localhost is also a secure location so we override it with our own strategy implements the static localhost detection algorithm in firefox https searchfox org mozilla central rev d4d7611ee4dd0003b492b865bc5988a4e6afc985 netwerk dns dns cpp 205 218
from http import cookiejar


_LOCALHOST = 'localhost'
_LOCALHOST_SUFFIX = '.localhost'


class HTTPieCookiePolicy(cookiejar.DefaultCookiePolicy):
    def return_ok_secure(self, cookie, request):
        

        is_secure_protocol = super().return_ok_secure(cookie, request)
        if is_secure_protocol:
            return True

        
        
        
        
        return self._is_local_host(cookiejar.request_host(request))

    def _is_local_host(self, hostname):
        
        
        return hostname == _LOCALHOST or hostname.endswith(_LOCALHOST_SUFFIX)