简体   繁体   English

重复使用httplib.HTTPSConnection对象时避免CannotSendRequest异常

[英]Avoiding CannotSendRequest exceptions when re-using httplib.HTTPSConnection objects

My code makes a bunch of https calls using httplib. 我的代码使用httplib进行了一堆https调用。 I want to re-use the httplib.py connection object, but if I do, I sometimes get CannotSendRequest exceptions because the connection ends up in a strange state because some other bit of code blows up mid-way through a request. 我想重新使用httplib.py连接对象,但是如果这样做,我有时会收到CannotSendRequest异常,因为连接最终以一种奇怪的状态结束,因为其他一些代码在请求过程中被CannotSendRequest了。 So what I want is a way to cache the connection object such that if it's in a valid state we re-use it, but re-connect if it's not in a valid state. 所以我想要的是一种缓存连接对象的方法,这样,如果连接对象处于有效状态,我们就可以重用它,但是如果连接对象不是处于有效状态,则可以重新连接。 I don't see a public method on httplib.HTTPSConnection to tell me if the connection is in a valid state or not. 我没有在httplib.HTTPSConnection上看到公用方法来告诉我连接是否处于有效状态。 And it's not easy for me to catch the CannotSendRequest exception because it could happen in lots of places in the code. 对于我来说,捕捉CannotSendRequest异常并不容易,因为它可能发生在代码的很多地方。 So what I'd like to do is something like this: 所以我想做的是这样的:

CONNECTION_CACHE = None

def get_connection():
    global CONNECTION_CACHE 

    if (not CONNECTION_CACHE) or (CONNECTION_CACHE.__state != httlib._CS_IDLE):
        CONNECTION_CACHE = httplib.HTTPSConnection(SERVER)

    return CONNECTION_CACHE

but this fails because __state is private. 但这失败了,因为__state是私有的。 Is there any way to do this? 有什么办法吗? I could patch httplib to expose a is_in_valid_state() method, but I'd rather avoid patching the base python libraries if I can. 我可以修补httplib以公开is_in_valid_state()方法,但如果可以的话,我宁愿避免修补基本python库。

(Touching private attribute is a bad idea, viewer discretion is advised.) (触摸私有属性是个坏主意,建议观众自行决定。)

> Private name mangling: When an identifier that textually occurs in a
> class definition begins with two or more underscore characters and
> does not end in two or more underscores, it is considered a private
> name of that class. Private names are transformed to a longer form
> before code is generated for them. The transformation inserts the
> class name in front of the name, with leading underscores removed, and
> a single underscore inserted in front of the class name. For example,
> the identifier __spam occurring in a class named Ham will be
> transformed to _Ham__spam. This transformation is independent of the
> syntactical context in which the identifier is used. If the
> transformed name is extremely long (longer than 255 characters),
> implementation defined truncation may happen. If the class name
> consists only of underscores, no transformation is done.

Thus conn._HTTPSConnection__state is the answer 因此conn._HTTPSConnection__state是答案

In Python there is no such thing as "private" or "public". 在Python中,没有“私有”或“公共”之类的东西。 Your code is failing somewhere else. 您的代码在其他地方失败。

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

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