Skip to content

Instantly share code, notes, and snippets.

@richm
Last active December 10, 2018 23:34
Show Gist options
  • Save richm/4865049ba17bd946a3c84959ec017253 to your computer and use it in GitHub Desktop.
Save richm/4865049ba17bd946a3c84959ec017253 to your computer and use it in GitHub Desktop.
python https client
import ssl
import httplib
import pprint
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH, "/etc/pki/tls/certs/ca-bundle.crt")
ctx.verify_mode = ssl.CERT_OPTIONAL
c = httplib.HTTPSConnection("www.google.com:443", context=ctx)
c.request("GET", "/")
print c.getresponse().read()[:80]
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH, "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")
pprint.pprint(ctx.get_ca_certs())
# https://docs.python.org/2/library/ssl.html#ssl.SSLContext.verify_mode
# require server cert and validate issuer/CA chain
ctx.verify_mode = ssl.CERT_OPTIONAL
c = httplib.HTTPSConnection("kubernetes.default.svc:443", context=ctx)
try:
c.request("GET", "/")
print c.getresponse().read()
except Exception, e:
print "Could not connect to kubernetes.default.svc:443:", e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment