title: Verify Python socket is encrypted
body: <p>I've made a TCP echo client/server with bits from <a href="https://pymotw.com/2/socket/tcp.html" rel="nofollow">here</a> and <a href="http://carlo-hamalainen.net/blog/2013/1/24/python-ssl-socket-echo-test-with-self-signed-certificate" rel="nofollow">here</a>. How can I verify that the socket is encrypted and cannot be intercepted and read? I've tried using Wireshark, but both SSL and non-SSL show encrypted packets. This makes me question their validity.</p>
  
  <p><strong>Client:</strong></p>
  
  <pre><code>import socket
  import ssl
  
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server_address = ('HOSTNAME', 10000)
  print('Connecting to %s port %s' % server_address)
  sock.connect(server_address)
  ssl_sock = ssl.wrap_socket(
          sock,
          ssl_version = ssl.PROTOCOL_TLSv1_2
  )
  
  try:
      message = str.encode(input('&gt;&gt;&gt; '))
      print('Sending "%s"' % message)
      ssl_sock.sendall(message)
  
      amount_received = 0
      amount_expected = len(message)
  
      while amount_received &lt; amount_expected:
          data = ssl_sock.recv(16)
          amount_received += len(data)
          print('Received "%s"' % data)
  
  finally:
      print('Closing socket')
      ssl_sock.close()
  </code></pre>
  
  <p><strong>Server:</strong></p>
  
  <pre><code>import socket
  import ssl
  
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server_address = ('HOSTNAME', 10000)
  print('Starting up on %s port %s' % server_address)
  sock.bind(server_address)
  sock.listen(1)
  
  while True:
      print('Waiting for a connection')
      try:
          connection, client_address = sock.accept()
          stream = ssl.wrap_socket(
              connection,
              server_side = True,
              certfile = 'fullchain.pem',
              keyfile = 'privkey.pem',
              ssl_version = ssl.PROTOCOL_TLSv1_2
          )
      except KeyboardInterrupt:
          print('\nStopping server')
          exit()
      except:
          print('ERROR')
          continue
  
      try:
          print('Connection from', client_address)
  
          while True:
              data = stream.recv(16)
              print('Received "%s"' % data)
              if data:
                  print('Sending data back to the client')
                  stream.sendall(data)
              else:
                  print('No more data from', client_address)
                  break
      finally:
          stream.close()
  </code></pre>
  
  <p>The server is running on my VPS and using the real certificate for HTTPS. Everything works, but I just want to be sure this test is secure before I build anything for real.</p>