[Python] SSL certficate verification when accessing a server via IP

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    Background for this question: In a project, we have some data on a shared webhosting. The DNS record for the site is with CloudFlare and is using its proxied DNS feature.

    So far so good, and creating a request like

    urllib.request.Request(
    'https://www.example.org/myfile.zip',
    headers={...}
    )


    and subsequent download works just fine.

    Now, because of (performance) side-effects of the proxy-DNS, I wanted to access the data using the direct IP of the webhosting. I have to supply 'Host' in the headers because it is a shared webshosting.

    When I do this using http, everything still works fine, i.e. like so:

    urllib.request.Request(
    "http://123.456.100.1/myfile.zip",
    headers={'Host': 'www.example.org'}
    )


    The problem is now when doing the same with https I get this error:

    urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: IP address mismatch, certificate is not valid for '123.456.100.1'


    Hence, finally the question:

    How can I download a file using https and directly using the IP, while still using the Host argument for the certificate verification?

    Continue reading...

Compartilhe esta Página