title: Python Requests Not Cleaning up Connections and Causing Port Overflow?
body: <p>I'm doing something fairly outside of my comfort zone here, so hopefully I'm just doing something stupid.</p>
  
  <p>I have an Amazon EC2 instance which I'm using to run a specialized database, which is controlled through a webapp inside of Tomcat that provides a REST API. On the same server, I'm running a Python script that uses the Requests library to make hundreds of thousands of simple queries to the database (I don't think it's possible to consolidate the queries, though I am going to try that next.)</p>
  
  <p>The problem: after running the script for a bit, I suddenly get a <strong>broken pipe</strong> error on my SSH terminal. When I try to log back in with SSH, I keep getting "operation timed out" errors. So I can't even log back in to terminate the Python process and instead have to reboot the EC2 instance (which is a huge pain, especially since I'm using ephemeral storage)</p>
  
  <p>My theory is that each time requests makes a REST call, it activates a pair of ports between  Python and Tomcat, but that it never closes the ports when it's done. So python keeps trying to grab more and more ports and eventually either somehow grabs away and locks the SSH port (booting me off), or it just uses all the ports and that causes the network system to crap out somehow (as I said, I'm out of my depth.)</p>
  
  <p>I also tried using <code>httplib2</code>, and was getting a similar problem.</p>
  
  <p>Any ideas? If my port theory is correct, is there a way to force requests to surrender the port when it's done? Or otherwise is there at least a way to tell Ubuntu to keep the SSH port off-limits so that I can at least log back in and terminate the process?</p>
  
  <p>Or is there some sort of best practice to using Python to make lots and lots of very simple REST calls?</p>
  
  <p>Edit:</p>
  
  <p>Solved...do:</p>
  
  <pre><code>s = requests.session()
  s.config['keep_alive'] = False
  </code></pre>
  
  <p>Before making the request to force Requests to release connections when it's done. </p>