怎样用原生的urllib2+httplib请求Https-创新互联

本篇文章给大家分享的是有关怎样用原生的urllib2+httplib请求Https,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联公司主营酉阳土家族苗族网站建设的网络公司,主营网站建设方案,app软件定制开发,酉阳土家族苗族h5小程序设计搭建,酉阳土家族苗族网站营销推广欢迎酉阳土家族苗族等地区企业咨询

环境

python2.7.5

# https server 生成证书

/tupian/20230522/8418111.html       # https server使用的key

ca_certs # https server使用的ca

cert        # 在 浏览器下载的证书, windows下载后linux可以使用

### 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/

# /tupian/20230522/using-client-certificates-with-urllib2 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 TO!??!

    '''

    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:

    /tupian/20230522/httplib.py   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()

以上就是怎样用原生的urllib2+httplib请求Https,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联-成都网站建设公司行业资讯频道。


当前名称:怎样用原生的urllib2+httplib请求Https-创新互联
标题URL:http://csdahua.cn/article/hdsso.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流