2010-07-26 2 views
1

내 코드는 httplib을 사용하여 많은 https 호출을 만듭니다. httplib.py 연결 개체를 다시 사용하고 싶지만 코드를 다른 방법으로 요청할 때 중간에 코드가 부 풀리기 때문에 연결이 이상한 상태로 끝나기 때문에 가끔 CannotSendRequest 예외가 발생합니다. 그래서 내가 원하는 것은 연결 객체를 캐시하는 방법입니다. 유효한 상태에 있으면 다시 사용하지만 유효 상태가 아닌 경우 다시 연결하도록 연결 객체를 캐시하는 방법입니다. 연결이 유효한 상태인지 여부를 알려주는 공개 방법이 httplib.HTTPSConnection에 표시되지 않습니다. 그리고 코드에서 많은 부분에서 CannotSendRequest 예외가 발생할 수 있으므로 예외를 잡는 것이 쉽지 않습니다. 그래서 내가하고 싶은 것을이 같은 것입니다 :httplib.HTTPSConnection 객체를 다시 사용할 때 CannotSendRequest 예외 방지

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 

하지만 __state 개인이기 때문에이 실패합니다. 이 일을 할 수있는 방법이 있습니까? is_in_valid_state() 메서드를 노출하기 위해 httplib을 패치 할 수는 있지만 가능하다면 기본 Python 라이브러리를 패치하지 않는 편이 좋습니다.

답변

-3

파이썬에는 "개인"또는 "공용"같은 것이 없습니다. 코드가 다른 곳에서 실패하고 있습니다.

0

(감동 개인 속성이 나쁜 생각, 뷰어 재량이 좋습니다.) 따라서

> 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. 

conn._HTTPSConnection__state 대답

입니다
관련 문제