简体   繁体   中英

closing a socks/proxy connection in python

I am writing some code that uses poplib and imaplib to collect emails through a proxy server.

I use the following to set up a proxy connection:-

import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,proxy_ip,port,True)
socket.socket = socks.socksocket

Which I got from the stackoverflow post:-

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

Then I make my connection with the email server:-

server = poplib.POP3(self.host, self.port)
server.user(self.username)
server.pass_(self.password)

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.

An example is:-

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.

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.

I know how to close the pop3 connection:-

server.quit()

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

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

No special action is required. When you close the POP connection, the proxy connection will close automatically, since it's only needed while you are connected to something through the proxy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM