0

一般来说,我对网络真的很陌生,我正在尝试在 python 服务器和客户端之间建立一个简单的交换。

这是服务器的代码

import socket, ssl

def do_something(connstream, data):
print "HALLO"

def deal_with_client(connstream):
    data = connstream.read()
    # null data means the client is finished with us
    while data:
        if not do_something(connstream, data):
            # we'll assume do_something returns False
            # when we're finished with client
            break
         data = connstream.read()
     # finished with client

bindsocket = socket.socket()
bindsocket.bind(('127.0.0.1', 10024))
bindsocket.listen(5)


while True:
    newsocket, fromaddr = bindsocket.accept()
    print "Setting up connection"
    connstream = ssl.wrap_socket(newsocket,
                             server_side=True,
                             ca_certs=None,
                             certfile="cert.pem",
                             keyfile="privatekey.pem",
                             ssl_version=ssl.PROTOCOL_TLSv1)
    try:
        deal_with_client(connstream)
    finally:
        connstream.shutdown(socket.SHUT_RDWR)
        connstream.close()

这是client.py的代码

 import socket, ssl

 clientsocket = socket.socket()
 ssl_sock = ssl.wrap_socket(clientsocket,
                       certfile="cert.pem",
                       cert_reqs=ssl.CERT_REQUIRED)
 ssl_sock.connect(('127.0.0.1', 10024))

 print ssl_sock.getpeername()
 print ssl_sock.getpeercert()

 data = ssl_sock.recv(1024)
 ssl_sock.close()

 print 'Received', repr(data)

我使用 openssl 生成了“cert.pem”和“privatekey.pem”。

Traceback (most recent call last):
 File "server.py", line 30, in <module>
    ssl_version=ssl.PROTOCOL_TLSv1)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 344, in wrap_socket
    ciphers=ciphers)
   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 121, in __init__
    self.do_handshake()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 283, in do_handshake
    self._sslobj.do_handshake()
 ssl.SSLError: [Errno 8] _ssl.c:499: EOF occurred in violation of protocol

我想知道是否以某种方式知道更多的人可以为我指明正确的方向。我真的很想使用 SSL btw 来做到这一点,但如果这是更好的方法,我愿意切换到 TLS。

4

1 回答 1

0

可能是套接字未使用兼容的 ssl 版本运行,您也应该在客户端中放置“ssl.PROTOCOL_TLSv1”兼容版本(或将其从服务器中删除并使用默认值)。谷歌搜索你可以找到许多套接字通信的例子

于 2013-06-21T14:18:39.390 回答