我跟着去了
Python SSL socket echo test with self-signed certificate
blog测试简单的SSL套接字连接。我生成了一个自签名证书,并使用上面的Python代码进行了测试。
一切都如所述,但问题是,当我使用Wireshark监视网络数据包时,我看不到任何SSL流量。我看到的只是普通的TCP数据包,但我希望看到SSL协议被使用。我错过了什么吗?
为了完整起见,我添加了代码:
客户端.py
import socket, ssl, pprint
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Require a certificate from the server. We used a self-signed certificate
# so here ca_certs must be the server certificate itself.
ssl_sock = ssl.wrap_socket(s,
ca_certs="server.crt",
cert_reqs=ssl.CERT_REQUIRED)
ssl_sock.connect(('localhost', 10023))
print repr(ssl_sock.getpeername())
print ssl_sock.cipher()
print pprint.pformat(ssl_sock.getpeercert())
ssl_sock.write("boo!")
if False: # from the Python 2.7.3 docs
# Set a simple HTTP request -- use httplib in actual code.
ssl_sock.write("""GET / HTTP/1.0\r
Host: www.verisign.com\n\n""")
# Read a chunk of data. Will not necessarily
# read all the data returned by the server.
data = ssl_sock.read()
# note that closing the SSLSocket will also close the underlying socket
ssl_sock.close()
服务器.py
import socket, ssl
bindsocket = socket.socket()
bindsocket.bind(('', 10023))
bindsocket.listen(5)
def do_something(connstream, data):
print "do_something:", data
return False
def deal_with_client(connstream):
data = connstream.read()
while data:
if not do_something(connstream, data):
break
data = connstream.read()
while True:
newsocket, fromaddr = bindsocket.accept()
connstream = ssl.wrap_socket(newsocket,
server_side=True,
certfile="server.crt",
keyfile="server.key")
try:
deal_with_client(connstream)
finally:
connstream.shutdown(socket.SHUT_RDWR)
connstream.close()
Wireshark截图: