Python socket ssl连接

时间:2016-04-25 19:41:01

标签: python sockets ssl connection certificate

以下是senario:

服务器通过主机10.10.10.1侦听两个端口9999,9998 客户端口9999端口控制器的端口9998 我能够将客户端和控制器连接到服务器,并在没有ssl套接字的情况下从控制器控制客户端。 我想使用加密套接字我添加了ssl证书套接字连接,客户端也没有通过SSL_SOCK作为控制器连接到服务器。 感谢您的帮助或建议。

服务器:

    s=socket(AF_INET, SOCK_STREAM)
    s.bind(("10.10.10.1",9999))
    s.listen(5)
    port = 9999
    password = "password"
    bridgeport = 9998

    allConnections = []
    allAddresses = []

    def getConnections():
        for item in allConnections:
            item.close()

        del allConnections[:]
        del allAddresses[:]

        while 1:
            try:
                q,addr=s.accept()
                connstream = ssl.wrap_socket(q, server_side=True, certfile="server.crt", keyfile="server.key", ssl_version=ssl.PROTOCOL_SSLv23) 
                connstream.setblocking(1)
                allConnections.append(connstream)
                allAddresses.append(addr)

            except:
                print "YOU ARE NOT CONNECTED!! TRY AGAIN LATER"
                time.sleep(10.0)
                break
def main():
    bridge=socket(AF_INET, SOCK_STREAM)
    bridge.bind(("10.10.10.1",9998))
    bridge.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    while 1:
        bridge.listen(1)
        q,addr=bridge.accept()
        bridgestream = ssl.wrap_socket(q, server_side=True, certfile="server.crt", keyfile="server.key", ssl_version=ssl.PROTOCOL_SSLv23)

        cpass = bridgestream.recv(4096)
        if (cpass == password):
            print "Controller is connected"
        else:
            print "Controller not conected"
try:        
    main()
except:
    try:

        del allConnections[:]
        del allAddresses[:]
    except:
        pass

控制器:

host = '10.10.10.1'
port = 9998
password = "password"


def main():

    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ssl_sock = ssl.wrap_socket(s, ca_certs="server.crt", cert_reqs=ssl.CERT_REQUIRED)
        ssl_sock.connect((host, port))
    except:
        sys.exit("[ERROR] Can't connect to server")

    ssl_sock.sendall(password)

客户端:

host = "10.10.10.1"
port = 9999
def main(host, port):
    while 1:
        connected = False
        while 1:
            while (connected == False):
                try:

                    print host
                    print port
                    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    print s
                    ssl_sock = ssl.wrap_socket(s, ca_certs="server.crt", cert_reqs=ssl.CERT_REQUIRED)
                    print "ssl_sock"
                    ssl_sock.connect((host,port))
                    print "Connected"
                    connected = True
                except:
                    print "The client not Connected yet"
                    time.sleep(5)
while 1:
    try:
        main(host, port)
    except:
        time.sleep(5)

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,并提出了两种方法:

  1. 首先尝试使用原始TCP socket(不使用ssl.wrap_socket())。如果可行,请使用诸如netstat之类的工具来诊断使用了哪些端口和IP地址(以防万一)。
  2. 尝试在两边都创建一个SSLContext,如下所示:
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
ssl_context.verify_mode = ssl.CERT_NONE
stream_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_socket = ssl_context.wrap_socket(stream_socket, server_hostname=hostname)
_socket.connect((hostname, port))

换句话说,不要先强制使用证书,然后尝试指定协议。希望这会有所帮助!