中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎樣用原生的urllib2+httplib請求Https

發布時間:2021-12-04 16:13:08 來源:億速云 閱讀:269 作者:柒染 欄目:互聯網科技

本篇文章給大家分享的是有關怎樣用原生的urllib2+httplib請求Https,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

環境

python2.7.5

# https server 生成證書

https://www.cnblogs.com/loleina/p/8418111.html

# HTTPSConnection

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

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

    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()

以上就是怎樣用原生的urllib2+httplib請求Https,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

英吉沙县| 五大连池市| 稷山县| 云霄县| 比如县| 静乐县| 那曲县| 射阳县| 修水县| 霍山县| 乌兰察布市| 宁德市| 开原市| 盘山县| 赤峰市| 宜君县| 西华县| 望江县| 兰州市| 深水埗区| 清水县| 信丰县| 灵丘县| 田阳县| 慈溪市| 绵竹市| 澎湖县| 南通市| 彭阳县| 洮南市| 靖西县| 清徐县| 左权县| 文成县| 洞头县| 铜梁县| 华蓥市| 田林县| 南靖县| 无锡市| 精河县|