import socket, ssl, pprint
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# require a certificate from the server
ssl_sock = ssl.wrap_socket(s,
# http://curl.haxx.se/ca/cacert.pem
ca_certs="cacert.pem",
cert_reqs=ssl.CERT_REQUIRED)
ssl_sock.connect(('www.verisign.com', 443))
print repr(ssl_sock.getpeername())
print ssl_sock.cipher()
print pprint.pformat(ssl_sock.getpeercert())
# Set a simple HTTP request use httplib in actual code.
ssl_sock.write("""GET / HTTP/1.0\r
Host: www.verisign.com\r\n\r\n""")
# Read a chunk of data. Will not necessarily
# read all the data returned by the server.
data = ssl_sock.read()
print repr(data)[:79]
# note that closing the SSLSocket will also close the underlying socket
ssl_sock.close()
这取决于你将如何处理结果。
httplib.HTTPSConnection
有set_debuglevel
方法,该方法允许将响应数据打印到stdout(尽管每行都加了前缀)。出于调试目的,这正是我需要的。不确定,这是你需要的。在Example from the docs:
输出
^{pr2}$Example with M2Crypto
相关问题 更多 >
编程相关推荐