0
A
回答
3
我不確定使用urllib2
。不幸的是,我可以找到關於此最近的信息是this link和相關代碼:
import socket
import ssl
HOST = "www.google.com"
PORT = 443
# replace HOST name with IP, this should fail connection attempt
HOST = socket.getaddrinfo(HOST, PORT)[0][4][0]
print(HOST)
# create socket and connect to server
# server address is specified later in connect() method
sock = socket.socket()
sock.connect((HOST, PORT))
# wrap socket to add SSL support
sock = ssl.wrap_socket(sock,
# flag that certificate from the other side of connection is required
# and should be validated when wrapping
cert_reqs=ssl.CERT_REQUIRED,
# file with root certificates
ca_certs="cacerts.txt"
)
# security hole here - there should be an error about mismatched host name
# manual check of hostname
cert = sock.getpeercert()
for field in cert['subject']:
if field[0][0] == 'commonName':
certhost = field[0][1]
if certhost != HOST:
raise ssl.SSLError("Host name '%s' doesn't match certificate host '%s'"
% (HOST, certhost))
雖然文件中的註釋是相當廣泛的,在第一個鏈接的維基還列出了這些指令:
要驗證證書是否與請求的站點匹配,您需要檢查證書的
subject
中的commonName
字段。該信息可以通過getpeercert()
包裝套接字的方法進行訪問。您將需要
cacerts.txt
文件,其中包含放置在腳本旁邊的根證書 - 請參閱下面的如何獲取更新列表。要檢查證書驗證是否有效 - 請使用https://www.debian-administration.org/
的HOST
名稱。此網站的證書未由來自cacerts.txt
的任何根證書籤名,因此您將收到錯誤消息。
你可以看着pyopenssl module爲好,根據我張貼的第一環節,因爲它可以用來以這種方式來驗證SSL證書:
import socket
from OpenSSL import SSL
HOST = "www.google.com"
PORT = 443
# replace HOST name with IP, this should fail connection attempt,
# but it doesn't by default
HOST = socket.getaddrinfo(HOST, PORT)[0][4][0]
print(HOST)
# uses HOST
def verify_cb(conn, x509, errno, errdepth, retcode):
"""
callback for certificate validation
should return true if verification passes and false otherwise
"""
if errno == 0:
if errdepth != 0:
# don't validate names of root certificates
return True
else:
if x509.get_subject().commonName != HOST:
return False
else:
return False
context = SSL.Context(SSL.SSLv23_METHOD)
context.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb)
context.load_verify_locations("cacerts.txt")
# create socket and connect to server
sock = socket.socket()
sock = SSL.Connection(context, sock)
sock.connect((HOST, PORT))
sock.do_handshake()
根據第一文檔鏈接,這些示例需要here的最新證書版本。
相關問題
- 1. 有沒有一種簡單的方法來檢查字段是否爲null?
- 2. 我是否需要簡單社區網站的SSL證書?
- 3. 有沒有辦法檢測一個網站是否使用SPDY?
- 4. 沒有SSL證書?
- 5. 有沒有簡單的方法來檢查基本類型
- 6. 簡單的方法來檢查FormatString是否有效?
- 7. 如何使網站SSL證書有效?
- 8. 有沒有簡單的方法來驗證複製文章是否正確?
- 9. 有沒有更簡單的方法來查看一個字段是否有一定的價值?
- 10. 是否有一個很好的簡單方法來檢查Javascript中的變量是否有值?
- 11. 有沒有一種簡單的方法來檢查傳入的呼叫者是否是Android中的聯繫人?
- 12. 沒有有效的SSL證書,HTTPS連接是否安全?
- 13. 有沒有一種簡單的方法來拼寫檢查與.NET中的TinyMCE
- 14. 是否有.net方法檢查用戶密碼是否簡單
- 15. 有沒有購買SSL證書的ERR_INSECURE_RESPONSE解決方法?
- 16. Android和SSL - 檢查證書有效性
- 17. 有沒有一種簡單的方法來document.createElement多個元素?
- 18. 有沒有一種簡單的方法來重複一個PHP的MySQL查詢?
- 19. SSL證書沒有通過
- 20. 在Rails中,是否有一種簡單的方法來檢查ActiveRecord的屬性?
- 21. 我的網站沒有顯示鎖(SSl證書)
- 22. 向沒有SSL證書的網站提供加密連接
- 23. 有沒有一種簡單的方法來匹配jQuery驗證3個字段
- 24. SSL證書1個網站
- 25. 是否有一個有效的\簡單的方法在Direct3D
- 26. 是否有一個簡單的通用出站點擊跟蹤網頁方法?
- 27. Rails - 有沒有簡單的方法來檢查英國夏令時和GMT是否有所不同?
- 28. 有沒有一種簡單的方法來檢查對象是否可以在python中使用JSON序列化?
- 29. 是否有簡單的方法來檢索Google財經數據?
- 30. 有沒有一個簡單的方法來使用oAuth2沒有AccountManager?
如果我的回答很有幫助,您可以通過點擊旁邊的複選標記來接受它。這可以讓更多的人看到這是一個有用的答案。 – 2012-07-20 16:06:55