python - Avoiding CannotSendRequest exceptions when re-using httplib.HTTPSConnection objects -


my code makes bunch of https calls using httplib. want re-use httplib.py connection object, if do, cannotsendrequest exceptions because connection ends in strange state because other bit of code blows mid-way through request. want way cache connection object such if it's in valid state re-use it, re-connect if it's not in valid state. don't see public method on httplib.httpsconnection tell me if connection in valid state or not. , it's not easy me catch cannotsendrequest exception because happen in lots of places in code. i'd 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 fails because __state private. there way this? patch httplib expose is_in_valid_state() method, i'd rather avoid patching base python libraries if can.

(touching private attribute bad idea, viewer discretion advised.)

> private name mangling: when identifier textually occurs in > class definition begins 2 or more underscore characters , > not end in 2 or more underscores, considered private > name of class. private names transformed longer form > before code generated them. transformation inserts > class name in front of name, leading underscores removed, , > single underscore inserted in front of class name. example, > identifier __spam occurring in class named ham > transformed _ham__spam. transformation independent of > syntactical context in identifier used. if > transformed name extremely long (longer 255 characters), > implementation defined truncation may happen. if class name > consists of underscores, no transformation done. 

thus conn._httpsconnection__state answer


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -