Python3:ResourceWarning Unclosed

时间:2015-12-29 20:06:01

标签: python python-3.x ssl cloudflare

我有一个用Python编码的Reddit Bot,有时我会收到以下错误:

  

sys:1:ResourceWarning:unclosed ssl.SSLSocket fd = 4,family = AddressFamily.AF_INET,type = 2049,proto = 6,laddr =('192.168.1.113',55513),raddr =('198.41.209.140 ',443)>

192.168.1.113是我的本地IP,198.41.209.140显然是CloudFlare的IP

我有时会得到以下错误,这让我觉得问题与SSL有关吗?

  

错误EOF违反协议(_ssl.c:598)

如何解决此问题?

编辑:如何查看代码的哪一部分导致此问题?我在try /中有一个例外,除了打印所有例外:

except Exception as e:
    print("Login/API Error", e)

但我得到的所有错误都没有,这意味着它不是一个例外吗?

1 个答案:

答案 0 :(得分:2)

我没有在自己的代码中使用套接字,因此以下内容基于我的一般Python知识。

套接字文档并未说明object Test { import cats.implicits._ import cats.data.XorT import scala.concurrent.Future type Cause = String case class DataWithContext[+A](data: A, context: List[String]) //context never need to change trait Processor[-A, B] extends Serializable { def process: DataWithContext[A] ⇒ XorT[Future, Cause, B] } implicit class ProcessorOps[A, B](self: Processor[A, B]) { def >>[C](that: Processor[B, C]) = Con(self, that) def zip[C](that: Processor[A, C]) = Zip(self, that) } //concat two processors case class Con[A, B, C](a: Processor[A, C], b: Processor[C, B]) extends Processor[A, B] { def process: DataWithContext[A] ⇒ XorT[Future, Cause, B] = (pc: DataWithContext[A]) ⇒ a.process(pc).flatMap { c ⇒ b.process(pc.copy(data = c)) } } //zip two processors case class Zip[A, B, C](p1: Processor[A, B], p2: Processor[A, C]) extends Processor[A, (B, C)] { def process: DataWithContext[A] ⇒ XorT[Future, Cause, (B, C)] = (pc: DataWithContext[A]) ⇒ for { b ← p1.process(pc) c ← p2.process(pc) } yield (b, c) } //an example of a primitive Processor case object Count extends Processor[String, Int] { def process: DataWithContext[String] ⇒ XorT[Future, Cause, Int] = (dc: DataWithContext[String]) => XorT.pure[Future, Cause, Int](dc.data.length) } } 实例是上下文管理器,但该类具有必需的socket.socket__enter__方法。两者都是标准的,最小的实现。您可能希望将套接字或SSLSocket子类化为__exit__中的更多内容,例如打印替换回溯。但首先,您需要查看完整的回溯。子类__exit__继承了这两种方法。由于SSLSocket"包装"一个插座,显然有两个要关注的对象。因此,对于代码的顶级结构,我将从以下(显然未经测试)开始:

SSLSocket

您收到的错误消息应该以{{1​​}}为前缀。是吗?文档说"当SSL连接突然终止时,SSLError的子类被引发。通常,遇到此错误时,不应尝试重用底层传输。"这绝对是一个例外。

<preliminary code>
with socket.socket(...) as sock  # or other socket-returning function
  <more preparation>
  with ssl.wrap_socket(sock, ...) as SSLsock # or the context function
    <use SSLsock to communicate>

什么可能引发Cloudfare突然终止连接&#39;?协议处理中的一个错误; ssl.py中的错误?您的机器人是否违反了服务条款?

如果您无法在此处获得所需的所有答案,我会尝试SSLEOFError,也可以>>> issubclass(ssl.SSLEOFError, Exception) True 的新闻组python-list访问。它有参与者具有网络经验。