def _make_request( self, loc, headers, data = None, retry = True ):
last_e = None
utserver_retry = False
retries = 0
max_retries = self._retry_max if retry else 1
try:
while retries < max_retries or utserver_retry:
try:
self._request.data = data
self._connection.request( self._request.get_method( ), self._request.selector + loc, self._request.data, headers )
resp = self._connection.getresponse( )
if resp.status == 400:
last_e = utorrent.uTorrentError( resp.read( ).decode( "utf8" ).strip( ) )
# if uTorrent server alpha is bound to the same port as WebUI then it will respond with "invalid request" to the first request in the connection
# apparently this is no longer the case, TODO: remove this hack
if ( not self._utorrent or type( self._utorrent ) == utorrent.uTorrent.LinuxServer ) and not utserver_retry:
utserver_retry = True
continue
raise last_e
elif resp.status == 404 or resp.status == 401:
raise utorrent.uTorrentError( "Request {}: {}".format( loc, resp.reason ) )
elif resp.status != 200 and resp.status != 206:
raise utorrent.uTorrentError( "{}: {}".format( resp.reason, resp.status ) )
self._cookies.extract_cookies( resp, self._request )
if len( self._cookies ) > 0:
self._request.add_header( "Cookie", "; ".join(
["{}={}".format( utorrent._url_quote( c.name ), utorrent._url_quote( c.value ) ) for c in self._cookies] ) )
return resp
# retry when utorrent returns bad data
except ( http.client.CannotSendRequest, http.client.BadStatusLine ) as e:
last_e = e
self._connection.close( )
# name resolution failed
except socket.gaierror as e:
raise utorrent.uTorrentError( e.strerror )
# socket errors
except socket.error as e:
# retry on timeout
if str( e ) == "timed out": # some peculiar handling for timeout error
last_e = utorrent.uTorrentError( "Timeout after {} tries".format( max_retries ) )
self._connection.close( )
# retry after pause on specific windows errors
elif e.errno == 10053 or e.errno == 10054:
# Windows specific socket errors:
# 10053 - An established connection was aborted by the software in your host machine
# 10054 - An existing connection was forcibly closed by the remote host
last_e = e
self._connection.close( )
time.sleep( 2 )
elif e.errno == errno.ECONNREFUSED or e.errno == errno.ECONNRESET or errno == errno.EHOSTUNREACH:
raise utorrent.uTorrentError( e.strerror )
else:
raise e
retries += 1
if last_e:
raise last_e
except Exception as e:
self._connection.close( )
raise e
return None
评论列表
文章目录