简体   繁体   English

在Python中关闭袜子/代理连接

[英]closing a socks/proxy connection in python

I am writing some code that uses poplib and imaplib to collect emails through a proxy server. 我正在编写一些代码,这些代码使用poplib和imaplib通过代理服务器收集电子邮件。

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:- 我从stackoverflow帖子中得到的:-

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. poplib库抛出一个无法连接的异常。 I catch the exception in the unittest, then move on to the next unittest, trusting the poplib library would properly close my previous connection. 我在单元测试中捕获到异常,然后继续进行下一个单元测试,并相信poplib库将正确关闭我以前的连接。

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:- 我知道如何关闭pop3连接:-

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. 当您关闭POP连接时,代理连接将自动关闭,因为仅在通过代理连接到某物时才需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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