【Python】用原生的urllib2+httplib請求Https
轉載: https://blog.csdn.net/ns2250225/article/details/79528827
環境
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
#
#
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:
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(')
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()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26250550/viewspace-2564737/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Charles 代理https請求HTTP
- dart原生請求Dart
- React Native請求Https請求不通怎麼解決React NativeHTTP
- 原生ajax請求&JSONPJSON
- 瀏覽器如何將你的http請求轉為https請求?瀏覽器HTTP
- 小程式https請求,http網站升到httpsHTTP網站
- 用原生js封裝一個ajax請求方法JS封裝
- Python爬蟲:HTTPS請求與響應,以及抓包工具Fiddler的使用Python爬蟲HTTP
- Charles對Android手機Https請求的抓包AndroidHTTP
- 使用CloseableHttpClient 訪問 http 和https 的get請求HTTPclient
- 使用Python請求http/https時設定失敗重試次數PythonHTTP
- Charles 抓取 https 請求說明文件(mac)HTTPMac
- python requests get請求 如何獲取所有請求Python
- 初探計算機網路之HTTPS請求計算機網路HTTP
- python做http請求PythonHTTP
- python爬蟲請求頭Python爬蟲
- 使用charles嗅探https請求,你的API並不安全HTTPAPI
- HTTPS請求筆記- SSL安全通道驗證問題HTTP筆記
- 『言善信』Fiddler工具 — 15、使用Fiddler抓取HTTPS請求HTTP
- Python編寫多行Header請求的方法PythonHeader
- 請求OpenFeign的GET請求時,請求為何失敗?
- iOS的http/https請求——十分鐘學會Charles抓包iOSHTTP
- python-http請求帶AuthorizationPythonHTTP
- python傳送HTTP POST請求PythonHTTP
- python 使用 retrying 重試請求Python
- 原生js實現Ajax請求,包含get和postJS
- springboot部署到阿里雲,配置https,springboot專案同時支援http和https請求,阿里雲配置httpsSpring Boot阿里HTTP
- Python中get、post請求詳解(HTTP請求頭、狀態碼)PythonHTTP
- 有趣的請求引數/請求頭
- Java用HttpClient3傳送http/https協議get/post請求,傳送map,jsoJavaHTTPclient協議JS
- 騰訊雲:免費SSL證書實現https請求HTTP
- 配置Charles 設定手機代理並允許https請求HTTP
- 原生Js呼叫Fetch Api請求介面(新的Ajax解決方案)JSAPI
- android中將原生請求的介面url的cookie同步到webview中。AndroidCookieWebView
- 安卓端出現https請求失敗的一次問題排查安卓HTTP
- C#請求HTTPS地址的故障分析和TLS知識點總結C#HTTPTLS
- SpringMVC中如何傳送GET請求、POST請求、PUT請求、DELETE請求。SpringMVCdelete
- 用whistle實現Abort請求