title: Python - safely close socket connection on terminal from keyboard input besides cntrl-c?
body: <p>The server I'm communicating to has stream session slots and sometimes the stream session couldn’t opened because all stream session slots are used. I suspect its because I'm building a program that talks to the server and feel when I cntrl-c to force stop the stream it doesn't close safely keeping the stream session slot open for that instance connection on the server. So when I terminate the program dozens of times this error builds up</p>
  
  <p>I don't have much knowledge on Python Networking to know if cntrl-c is as clean as hitting a key and calling the method close_conn(sock) but I want to try.</p>
  
  <p>Suppose I'm running this on my terminal"</p>
  
  <pre><code> ...
      print("Now listening from server for data...\n")
        while True:
  
          received = sock.recv(1024) 
  
          #pseudo_code
          if (user_input == "e") close_conn(sock) 
  </code></pre>
  
  <p>How can for debugging purposes close the connection safely?  </p>