未经认证的客户端连接时,Python ssl服务器阻止

时间:2015-01-11 20:30:44

标签: python sockets python-3.x ssl python-multithreading

您好我已经写了一个echo ssl Server。我对未经认证的用户有疑问!当用户获得认证时,一切正常,并且多个用户可以与服务器通信。但是,当未经认证的用户来时,它会在接受请求时阻止服务器!有什么方法可以解决这个问题吗?有没有办法拒绝未经认证的客户? server是一个包含server_socket的类,当创建服务器时,它会将server_socket绑定到端口并开始监听。 ConnectionThread是一个负责处理与客户端通信的类。

s = Server(port, secure=SSL)
        while True:
            count += 1
            client_socket, addr = s.server_socket.accept()
            if s.secure:
                # when we have set the server to follow SSL protocol
                try:
                    ssl_client = ssl.wrap_socket(
                        client_socket, keyfile=keypath,
                        certfile=certpath, server_side=True,
                        cert_reqs=ssl.CERT_REQUIRED, ca_certs=certpath)
                    if ssl_client:
                        print("Connected to client through SSL" + str(count) + str(ssl_client.getpeername()))
                        print("CERTIFICATES: ")
                        print(pprint.pformat(ssl_client.getpeercert()))
                        client = ConnectionThread(ssl_client)
                        client.client_socket.sendall("Thanks for connecting to me\n".encode())
                        client.start()
                    else:
                        print(" connection request is refused! ")
                except ssl.SSLError as e:
                    print("connection to the client failed : " + str(e.args))
                    continue

#error after stopping the server main processes with Cntr+c
CTraceback (most recent call last): File "echo_server.py", line 74, in <module> cert_reqs=ssl.CERT_REQUIRED, ca_certs=certpath) File "/home/user/miniconda3/lib/python3.3/ssl.py", line 630, in wrap_socket ciphers=ciphers) File "/home/user/miniconda3/lib/python3.3/ssl.py", line 346, in init self.do_handshake() File "/home/user/miniconda3/lib/python3.3/ssl.py", line 553, in do_handshake self._sslobj.do_handshake() KeyboardInterrupt 

0 个答案:

没有答案