<p>您可以使用某种类型的<a href="http://en.wikipedia.org/wiki/Transport_Layer_Security" rel="nofollow noreferrer">Transport Layer Security</a>,简单地说是用于任何网络套接字的TLS/SSL包装。在</p>
<p><a href="http://docs.python.org/2/library/ssl.html" rel="nofollow noreferrer">All the documentation here</a>。在</p>
<p>简单的客户端SSL加密,如演示的<a href="http://docs.python.org/2/library/ssl.html#client-side-operation" rel="nofollow noreferrer">here</a>:</p>
<pre><code>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,
ca_certs="/etc/ca_certs_file",
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()
# note that closing the SSLSocket will also close the underlying socket
ssl_sock.close()
</code></pre>
<p><a href="http://docs.python.org/2/library/ssl.html#examples" rel="nofollow noreferrer">More examples here.</a></p>
<p>另外,确保使用最新版本的算法,以避免加密不安全。在</p>
<p>你可能想看看这些问题:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/11323592/securing-a-udp-connection?rq=1">Securing a UDP connection</a></li>
<li><a href="https://stackoverflow.com/questions/8974627/how-long-is-an-open-secure-tcp-channel-secure?rq=1">How long is an open, secure, TCP channel secure?</a></li>
<li><a href="https://stackoverflow.com/questions/1888503/secure-connection-between-client-and-server?rq=1">Secure connection between client and server</a></li>
</ul>