'module' object has no attribute 'wrap_socket' when use ssl

Ho Yeung Lee jobmattcon at gmail.com
Sat Apr 15 04:41:53 EDT 2017


On Saturday, April 15, 2017 at 3:18:58 PM UTC+8, Peter Otten wrote:
> Ho Yeung Lee wrote:
> 
> > Python 2.7.6 (default, Jun 22 2015, 18:00:18)
> > [GCC 4.8.2] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> import ssl
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> >   File "/home/martin/Documents/ssl.py", line 13, in <module>
> >     SSLError -- exception raised for I/O errors
> > AttributeError: 'module' object has no attribute 'wrap_socket'
> 
> Look at the traceback again -- the ssl.py you are importing is not the one 
> from the standard library, it's an arbitrary module, perhaps written by 
> "martin". Once you rename Martin's ssl.py to something else and also remove 
> the corresponding ssl.pyc you will be able to work with the desired ssl.py 
> from the stdlib.


thanks , it can run now,

but how to create a SSL server for testing it?
when I use google as server, it failed handshake

python crackssl.py
Traceback (most recent call last):
  File "crackssl.py", line 16, in <module>
    wrappedSocket.connect((HOST, PORT))
  File "/usr/lib/python2.7/ssl.py", line 433, in connect
    self._real_connect(addr, False)
  File "/usr/lib/python2.7/ssl.py", line 420, in _real_connect
    socket.connect(self, addr)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused
martin at ubuntu:~/Documents$ python crackssl.py
Traceback (most recent call last):
  File "crackssl.py", line 16, in <module>
    wrappedSocket.connect((HOST, PORT))
  File "/usr/lib/python2.7/ssl.py", line 433, in connect
    self._real_connect(addr, False)
  File "/usr/lib/python2.7/ssl.py", line 423, in _real_connect
    self.do_handshake()
  File "/usr/lib/python2.7/ssl.py", line 405, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [Errno 1] _ssl.c:510: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure


import socket
import ssl

# SET VARIABLES
packet, reply = "<packet>SOME_DATA</packet>", ""
HOST, PORT = 'www.google.com.hk', 443

# 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()


More information about the Python-list mailing list