This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Cannot close socket after ssl handshake failed
Type: Stage:
Components: IO Versions: Python 2.6, Python 2.5
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: metachris
Priority: normal Keywords:

Created on 2010-07-09 09:21 by metachris, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg109697 - (view) Author: Christian Hager (metachris) Date: 2010-07-09 09:21
When running a SSL socket server in Python 2.5 or 2.6 (didn't try with newer versions), new connections are setup with "ssl.wrap_socket()". Everything works fine if I connect a client with SSL, but if I just telnet to the server it's not possible to close the socket anymore (receiving data works fine through the non-ssl socket though).

I get a SSL exception like this: "SSLError: [Errno 1] _ssl.c:480: error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol". After that I am not able to close the socket, but still can send data on the non-ssl-wrapped socket. How would I close this now open non-ssl socket?

Simplified code:

read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
for sock in read_sockets:
    if sock == server_socket:
        newsock, addr = server_socket.accept()

        # SSL Setup
        try:
            sslsock = ssl.wrap_socket(newsock, server_side=True, certfile="../cert.pem", keyfile="../cert.pem")
            CONNECTION_LIST.append(sslsock)
        except:
            print "ssl handshake failed"

            try: 
                newsock.send("bye\r\n") # succeeds
                newsock.close()         # doesn't work
msg109703 - (view) Author: Christian Hager (metachris) Date: 2010-07-09 09:51
Sorry for posting that issue -- I was able to solve it with newsock.shutdown(socket.SHUT_WR) ("newsock.shutdown(socket.SHUT_RD)" doesn't work for that).

# SSL Setup
try:
    sslsock = ssl.wrap_socket(newsock, server_side=True, certfile="../cert.pem", keyfile="../cert.pem")
    CONNECTION_LIST.append(sslsock)

except:
    print "ssl handshake failed"
    newsock.shutdown(socket.SHUT_WR)
    newsock.close()
History
Date User Action Args
2022-04-11 14:57:03adminsetgithub: 53457
2010-07-09 09:51:48metachrissetstatus: open -> closed

messages: + msg109703
2010-07-09 09:21:33metachriscreate