<s> from . base import Upstream <EOL> from tornado . iostream import IOStream <EOL> import socket <EOL> import logging <EOL> import functools <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class RemoteUpstream ( Upstream ) : <EOL> """<STR_LIT>""" <EOL> def initialize ( self ) : <EOL> self . socket = socket . socket ( self . _address_type , socket . SOCK_STREAM ) <EOL> self . stream = IOStream ( self . socket ) <EOL> self . stream . set_close_callback ( self . on_close ) <EOL> def do_connect ( self ) : <EOL> self . stream . connect ( self . dest , self . on_connect ) <EOL> @ property <EOL> def address ( self ) : <EOL> return self . socket . getsockname ( ) <EOL> @ property <EOL> def address_type ( self ) : <EOL> return self . _address_type <EOL> def on_connect ( self ) : <EOL> self . connection_callback ( self ) <EOL> on_finish = functools . partial ( self . on_streaming_data , finished = True ) <EOL> self . stream . read_until_close ( on_finish , self . on_streaming_data ) <EOL> def on_close ( self ) : <EOL> if self . stream . error : <EOL> self . error_callback ( self , self . stream . error ) <EOL> else : <EOL> self . close_callback ( self ) <EOL> def on_streaming_data ( self , data , finished = False ) : <EOL> if len ( data ) : <EOL> self . streaming_callback ( self , data , finished ) <EOL> def do_write ( self , data ) : <EOL> try : <EOL> self . stream . write ( data ) <EOL> except IOError as e : <EOL> self . close ( ) <EOL> def do_close ( self ) : <EOL> if self . socket : <EOL> logger . debug ( "<STR_LIT>" % self . socket ) <EOL> self . stream . close ( ) </s>
