1

I have a function that may call socket.close() to terminate the connection, and I want to check if it did. (I'm talking about my script - not if it was closed on the other end) For some reason socket._closed still returns False and socket._connected returns True.

Trying to read/write to it throws

*** ValueError: Read on closed or unwrapped SSL socket."

but I want to verify without try/except (which is pretty messy). E.g.:

sock.close()
if sock.is_closed:
    do_something()

Isn there a way to do that?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
user5908639
  • 51
  • 2
  • 6

1 Answers1

2

from When to not just use socket.close()

def recv_end(the_socket):
    End='SERVER WRONG MARKER'
    total_data=[];data='';got_end=False
    while True:
            data=the_socket.recv(8192)
            if not data: break
            if End in data:
                total_data.append(data[:data.find(End)])
                got_end=True
                break
            total_data.append(data)
            if len(total_data)>1:
                #check if end_of_data was split
                last_pair=total_data[-2]+total_data[-1]
                if End in last_pair:
                    total_data[-2]=last_pair[:last_pair.find(End)]
                    total_data.pop()
                    got_end=True
                    break
    return (got_end,''.join(total_data))

def basic_server(sock):
    got=[]
    got_end,data = recv_end(sock)
    if not got_end:  
        sock.send('ERROR:no end!') #<--- not possible w/close()
    else: sock.sendall(data*2)
    sock.shutdown(1)
    sock.close()

import socket
Port=4444
def start_server():
    sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.bind(('',Port))
    sock.listen(5)
    print 'started on',Port
    while True:
        newsock,address=sock.accept()
        basic_server(newsock)

def send_data(data):
    sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.connect(('localhost',Port))
    print 'connected'
    sock.sendall(data+'CLIENT WRONG MARKER')
    print 'sent',data
    sock.shutdown(1)
    print 'shutdown'
    result=[]
    while True:
       got=sock.recv(2)
       if not got: break
       result.append(got)
    sock.close()
    return ''.join(result)

if __name__=='__main__':
    start_server()

simpler, smaller solution

If by "this end" you meant server-side, this answer should help. If you meant client-side, the following code from this answer should work.

Community
  • 1
  • 1
Valkyrie
  • 841
  • 1
  • 9
  • 22
  • by "this end" I mean the current script itself. editing the question with an example... – user5908639 Feb 10 '16 at 15:48
  • It is good that @Chitoge handles both client and server scenarios. A "script" could be running either a server, or a client, or both. Server and Client are clearer terms than script. In, [Socket Programming HowTo](https://docs.python.org/2/howto/sockets.html#disconnecting) There are some important notes re: disconnecting. – OYRM Feb 10 '16 at 16:13
  • but that doesn't answer my question... My scenario is that I'm the client and I'm closing the socket and I want to verify I did. look at the example I gave in the question. – user5908639 Feb 11 '16 at 07:16