Given this portion of code, when pressing Ctrl+C the program returns error "name 'sock' is not defined.". I guess this is normal since sock.close()
is outside the class, but what should I do to prevent it ?
In my case it is about a client, not server, that asks for socket close.
import socket
class something(object):
def connect(self):
self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.tcp_ip, self.tcp_port))
# etc.
if __name__ == '__main__':
try:
app = something()
app.connect()
except KeyboardInterrupt:
pass
finally:
sock.close()