closing a socks/proxy connection in python
<p>I am writing some code that uses poplib and imaplib to collect emails through a proxy server.</p>

<p>I use the following to set up a proxy connection:-</p>

<pre><code>import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,proxy_ip,port,True)
socket.socket = socks.socksocket
</code></pre>

<p>Which I got from the stackoverflow post:-</p>

<pre><code>http://stackoverflow.com/questions/3386724/python-how-can-i-fetch-emails-via-pop-or-imap-through-a-proxy
</code></pre>

<p>Then I make my connection with the email server:-</p>

<pre><code>server = poplib.POP3(self.host, self.port)
server.user(self.username)
server.pass_(self.password)
</code></pre>

<p>I am testing my code in a unittest and have encountered a problem that I believe relates to my connection with the proxy not closing down properly.</p>

<p>An example is:-</p>

<p>I have set up the proxy connection and am trying to establish a connection with the email server. As part of the unittest I intentionally use an incorrect email server password.
The poplib library throws an exception that it can't connect. I catch the exception in the unittest, then move on to the next unittest, trusting the poplib library would properly close my previous connection.</p>

<p>My understanding is that this is not a good thing and that I should be ensuring the email and proxy server connections are properly closed.</p>

<p>I know how to close the pop3 connection:-</p>

<pre><code>server.quit()
</code></pre>

<p>But do not know how to close the connection with the proxy server or if I have to do so.</p>

<p>Could someone please help me with this question or with my understanding if that's where the problem lies :)</p>
