Opening a SSL socket connection in Python

前端 未结 4 1868
予麋鹿
予麋鹿 2020-11-27 12:30

I\'m trying to establish a secure socket connection in Python, and i\'m having a hard time with the SSL bit of it. I\'ve found some code examples of how to establish a conn

4条回答
  •  庸人自扰
    2020-11-27 12:59

    Ok, I figured out what was wrong. It was kind of foolish of me. I had two problems with my code. My first mistake was when specifying the ssl_version I put in TLSv1 when it should have been ssl.PROTOCOL_TLSv1. The second mistake was that I wasn't referencing the wrapped socket, instead I was calling the original socket that I have created. The below code seemed to work for me.

    import socket
    import ssl
    
    # SET VARIABLES
    packet, reply = "SOME_DATA", ""
    HOST, PORT = 'XX.XX.XX.XX', 4434
    
    # CREATE SOCKET
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(10)
    
    # WRAP SOCKET
    wrappedSocket = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1, ciphers="ADH-AES256-SHA")
    
    # CONNECT AND PRINT REPLY
    wrappedSocket.connect((HOST, PORT))
    wrappedSocket.send(packet)
    print wrappedSocket.recv(1280)
    
    # CLOSE SOCKET CONNECTION
    wrappedSocket.close()
    

    Hope this can help somebody!

提交回复
热议问题