@@ -18,43 +18,11 @@ def __init__(self, server, port=80, headers={}):
1818 self ._connect ()
1919
2020 def write (self , data , reconnect_on = ('' , 200 , )):
21- ''' Send `data` to the server in chunk-encoded form.
22- Check the connection before writing and reconnect
23- if disconnected and if the response status code is in `reconnect_on`.
24-
25- The response may either be an HTTPResponse object or an empty string.
21+ ''' Send `data` to the server in chunk-encoded form, verifying
22+ the connection is still open before doing so.
2623 '''
27-
28- if not self ._isconnected ():
29-
30- # Attempt to get the response.
31- response = self ._getresponse ()
32-
33- # Reconnect depending on the status code.
34- if ((response == '' and '' in reconnect_on ) or
35- (response and isinstance (response , httplib .HTTPResponse ) and
36- response .status in reconnect_on )):
37- self ._reconnect ()
38-
39- elif response and isinstance (response , httplib .HTTPResponse ):
40- # If an HTTPResponse was recieved then
41- # make the users aware instead of
42- # auto-reconnecting in case the
43- # server is responding with an important
44- # message that might prevent
45- # future requests from going through,
46- # like Invalid Credentials.
47- # This allows the user to determine when
48- # to reconnect.
49- raise Exception ("Server responded with "
50- "status code: {status_code}\n "
51- "and message: {msg}."
52- .format (status_code = response .status ,
53- msg = response .read ()))
54-
55- elif response == '' :
56- raise Exception ("Attempted to write but socket "
57- "was not connected." )
24+
25+ _verifyConnection (reconnect_on )
5826
5927 try :
6028 msg = data
@@ -67,9 +35,22 @@ def write(self, data, reconnect_on=('', 200, )):
6735 self .write (data )
6836
6937 def heartbeat (self , reconnect_on = ('' , 200 , )):
70- ''' Send `\n ` to the server
71- Check the connection before writing and reconnect
72- if disconnected and if the response status code is in `reconnect_on`.
38+ ''' Send `\n ` to the server, verifying the connection is still
39+ open before doing so.
40+ '''
41+
42+ _verifyConnection (reconnect_on )
43+
44+ try :
45+ # Send the \n
46+ self ._conn .send ('\n ' )
47+ except httplib .socket .error :
48+ self ._reconnect ()
49+ self .heartbeat ()
50+
51+ def _verifyConnection (self , reconnect_on ):
52+ ''' Check the connection, reconnect if disconnected and if the
53+ response status code is in `reconnect_on`.
7354
7455 The response may either be an HTTPResponse object or an empty string.
7556 '''
@@ -105,13 +86,6 @@ def heartbeat(self, reconnect_on=('', 200, )):
10586 raise Exception ("Attempted to write but socket "
10687 "was not connected." )
10788
108- try :
109- # Send the \n
110- self ._conn .send ('\n ' )
111- except httplib .socket .error :
112- self ._reconnect ()
113- self .heartbeat ()
114-
11589 def _connect (self ):
11690 ''' Initialize an HTTP connection with chunked Transfer-Encoding
11791 to server:port with optional headers.
0 commit comments