When should I explicitly call 'close()' on a socket in Python? -
i'm trying write simple python 2.7 function listen socket connection on localhost port.
question is: if bad happens when code responsible killing socket not left dangling out there? in cases socket error close socket me don't need call 'close()'?
i want avoid having start testing on different port because old 1 left open.
python code sample:
def getconn(port,timeout_secs): conn = '' try: host = '' s1 = socket.socket(socket.af_inet, socket.sock_stream) s1.settimeout(timeout_secs) s1.bind((host, port)) s1.listen(1) conn, addr = s1.accept() print('connected by', addr) except socket.timeout e: conn = 'socket timeout' except socket.error e: conn = 'other socket error' except: conn = 'deaddead' if s1: s1.close() return conn
and used here (say port argument 19111)...
def runtest01_normalpoll(port): conn = getconn(port, 10.0) if conn == 'socket timeout' or conn == 'other socket error' or conn == 'deaddead': print 'could not establish connection...why ask?', conn else: conn.close() print 'done...'
it seems work missing edge cases leave socket open or cause else bad happen?
Comments
Post a Comment