server certificate verification failed CAfile: /etc/ssl/certs/ca-certificates.crt
The error message says that according to the information in the specified CAfile, the certificate sent from this site does not pass verification.
For comparison, with the yandex site the same code works without problems:
import pycurl c = pycurl.Curl() c.setopt(c.URL, 'https://ya.ru') c.perform()
The curl -k command works for you as the -k key ( --insecure ) turns off checking, which allows third parties to eavesdrop / change messages from / to the site (makes the connection vulnerable to a MITM attack ). It is expected to just curl <url> error returns :
curl: (60) SSL certificate problem: self signed certificate
The private IP in the question is indicated, ask the site administrator for the certificate, which can be curl --cacert to curl --cacert . If you are unable to obtain a certificate directly, in order not to use an insecure connection with each connection, you can download the certificate once:
$ openssl s_client -prexit -servername $host -connect $host:443 </dev/null | openssl x509 >server.crt
where host=192.168.2.1 .
If we assume that the connection was not compromised when you received server.crt , then after that you can safely request the url:
$ curl --cacert server.crt https://$host/path?query
or in Python:
import pycurl c = pycurl.Curl() c.setopt(c.URL, url) c.setopt(c.CAINFO, 'server.crt') c.perform()
or using the requests module:
import requests r = requests.get(url, verify='server.crt') print(r.text)
or using urllib from the standard library:
import ssl import urllib.request context = ssl.create_default_context(cafile='server.crt') with urllib.request.urlopen(url, context=context) as r: print(r.read().decode(r.headers.get_content_charset('utf-8')))
Certificate validation is enabled by default since Python 2.7.9 and Python 3.4.3 .
To disable certificate verification ( not recommended ) for pycurl:
c.setopt(c.SSL_VERIFYPEER, 0) # default 1 c.setopt(c.SSL_VERIFYHOST, 0) # default 2
for module requests , you can pass verify=False to disable the protection.
To open yourself to the MITM attack using the standard library: urlopen(url, context=ssl._create_stdlib_context()) .