In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use native urllib2+httplib to request Https, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Environment
Python2.7.5
# https server generates certificates
Https://www.cnblogs.com/loleina/p/8418111.html
# HTTPSConnection
Key # key used by https server
Ca_certs # ca used by https server
Cert # the certificate downloaded in the browser can be used by linux after windows download
# https.py
Import urllib2, httplib, ssl, socket
DEFAULT_HTTP_TIMEOUT = 10 # seconds
# http://code.activestate.com/recipes/577548-https-httplib-client-connection-with-certificate-v/
# http://stackoverflow.com/questions/1875052/using-paired-certificates-with-urllib2
Class HTTPSClientAuthHandler (urllib2.HTTPSHandler):
''
Allows sending a client certificate with the HTTPS connection.
This version also validates the peer (server) certificate since, well...
WTF IS THE POINT OF SSL IF YOU DON "T AUTHENTICATE THE PERSON YOU" RE TALKING toddlers!
''
Def _ _ init__ (self, key=None, cert=None, ca_certs=None, ssl_version=None, ciphers=None):
Urllib2.HTTPSHandler.__init__ (self)
Self.key = key
Self.cert = cert
Self.ca_certs = ca_certs
Self.ssl_version = ssl_version
Self.ciphers = ciphers
Def https_open (self, req):
# Rather than pass in a reference to a connection class, we pass in
# a reference to a function which, for all intents and purposes
# will behave as a constructor
Return self.do_open (self.get_connection, req)
Def get_connection (self, host, timeout=DEFAULT_HTTP_TIMEOUT):
Return HTTPSConnection (host
Key_file = self.key
Cert_file = self.cert
Timeout = timeout
Ciphers = self.ciphers
Ca_certs = self.ca_certs)
Class HTTPSConnection (httplib.HTTPSConnection):
''
Overridden to allow peer certificate validation, configuration
Of SSL/ TLS version and cipher selection. See:
Http://hg.python.org/cpython/file/c1c45755397b/Lib/httplib.py#l1144
And `ssl.wrap_socket () `
''
Def _ _ init__ (self, host, * * kwargs):
Self.ciphers = kwargs.pop ('ciphers',None)
Self.ca_certs = kwargs.pop ('ca_certs',None)
Self.ssl_version = kwargs.pop ('ssl_version', ssl.PROTOCOL_SSLv23)
Httplib.HTTPSConnection.__init__ (self,host,**kwargs)
Def connect (self):
Sock = socket.create_connection ((self.host, self.port), self.timeout)
If self._tunnel_host:
Self.sock = sock
Self._tunnel ()
Self.sock = ssl.wrap_socket (sock
Keyfile = self.key_file
Certfile = self.cert_file
Ca_certs = self.ca_certs
Cert_reqs = ssl.CERT_REQUIRED if self.ca_certs else ssl.CERT_NONE)
# test.py
Import urllib2
Import urllib
Import https
Import ssl
Import json
Client_cert_key = "etcd-client-key.pem" # file path
Client_cert_pem = "etcd-client.pem" # file path
Ca_certs = "etcd-ca.pem" # file path
Handlers = []
Handlers.append (https.HTTPSClientAuthHandler (
Key = client_cert_key
Cert = client_cert_pem
Ca_certs = ca_certs
Ssl_version = ssl.PROTOCOL_SSLv23
Ciphers = 'TLS_RSA_WITH_AES_256_CBC_SHA'))
Http = urllib2.build_opener (* handlers)
# request https
# GET
Resp = http.open ('https://xxxx:2379/v2/members')
Data = resp.read ()
# POST
Req = urllib2.Request (url)
Data = urllib.urlencode (data)
Resp = http.open (req, data)
# PUT
Request = urllib2.Request (url, data=json_data)
Request.add_header ('Content-Type',' application/json')
Request.get_method = lambda: 'PUT'
Resp = http.open (request)
# DELETE
Request = urllib2.Request (url, data=data)
Request.get_method = lambda: 'DELETE'
Resp = http.open (request)
Resp.close ()
The above is how to use native urllib2+httplib to request Https. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.