演示python服务器-客户端交互

2024-04-19 08:20:13 发布

您现在位置:Python中文网/ 问答频道 /正文

一般来说,我对网络非常陌生,我正在尝试在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()

这是客户端.py在

^{pr2}$

我生成了“证书pem“以及”私钥.pem“使用openssl。在

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。在


Tags: inpyclientssldatawithlinesocket
1条回答
网友
1楼 · 发布于 2024-04-19 08:20:13

可能是套接字没有与兼容的ssl版本一起运行,您应该将一个“ssl.协议\u TLSv1“客户机中也有兼容版本(或从服务器中删除并使用默认值)。 你可以在谷歌上找到许多插座通讯的例子

相关问题 更多 >