@@ -77,6 +77,22 @@ def send(self, data):
7777 '''
7878 self ._vcr_request .body = (self ._vcr_request .body or '' ) + data
7979
80+ def close (self ):
81+ self ._restore_socket ()
82+ self ._baseclass .close (self )
83+
84+ def _restore_socket (self ):
85+ """
86+ Since some libraries (REQUESTS!!) decide to set options on
87+ connection.socket, I need to delete the socket attribute
88+ (which makes requests think this is a AppEngine connection)
89+ and then restore it when I want to make the actual request.
90+ This function restores it to its standard initial value
91+ (which is None)
92+ """
93+ if not hasattr (self , 'sock' ):
94+ self .sock = None
95+
8096 def _send_request (self , method , url , body , headers ):
8197 """
8298 Copy+pasted from python stdlib 2.6 source because it
@@ -132,6 +148,7 @@ def _send_output(self, message_body=None):
132148 if isinstance (message_body , str ):
133149 msg += message_body
134150 message_body = None
151+ self ._restore_socket ()
135152 self ._baseclass .send (self , msg )
136153 if message_body is not None :
137154 #message_body was not a string (i.e. it is a file) and
@@ -156,7 +173,10 @@ def getresponse(self, _=False):
156173 # Otherwise, we should send the request, then get the response
157174 # and return it.
158175
159- # make the request
176+ # restore sock's value to None, since we need a real socket
177+ self ._restore_socket ()
178+
179+ #make the actual request
160180 self ._baseclass .request (
161181 self ,
162182 method = self ._vcr_request .method ,
@@ -189,6 +209,8 @@ class VCRHTTPConnection(VCRConnectionMixin, HTTPConnection):
189209
190210 def __init__ (self , * args , ** kwargs ):
191211 HTTPConnection .__init__ (self , * args , ** kwargs )
212+ # see VCRConnectionMixin._restore_socket for the motivation here
213+ del self .sock
192214
193215
194216class VCRHTTPSConnection (VCRConnectionMixin , HTTPSConnection ):
@@ -203,3 +225,5 @@ class because HTTPConnection when this happens has been replaced by
203225 HTTPConnection .__init__ (self , * args , ** kwargs )
204226 self .key_file = kwargs .pop ('key_file' , None )
205227 self .cert_file = kwargs .pop ('cert_file' , None )
228+ # see VCRConnectionMixin._restore_socket for the motivation here
229+ del self .sock
0 commit comments