<s> """<STR_LIT>""" <EOL> import socket <EOL> from mod_pywebsocket import util <EOL> class ConnectionTerminatedException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class InvalidFrameException ( ConnectionTerminatedException ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class BadOperationException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UnsupportedFrameException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class InvalidUTF8Exception ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class StreamBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request ) : <EOL> """<STR_LIT>""" <EOL> self . _logger = util . get_class_logger ( self ) <EOL> self . _request = request <EOL> def _read ( self , length ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> read_bytes = self . _request . connection . read ( length ) <EOL> if not read_bytes : <EOL> raise ConnectionTerminatedException ( <EOL> '<STR_LIT>' % <EOL> ( length , ( self . _request . connection . remote_addr , ) ) ) <EOL> return read_bytes <EOL> except socket . error , e : <EOL> raise ConnectionTerminatedException ( <EOL> '<STR_LIT>' % <EOL> ( length , e ) ) <EOL> except IOError , e : <EOL> raise ConnectionTerminatedException ( <EOL> '<STR_LIT>' % <EOL> ( length , e ) ) <EOL> def _write ( self , bytes_to_write ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _request . connection . write ( bytes_to_write ) <EOL> except Exception , e : <EOL> util . prepend_message_to_exception ( <EOL> '<STR_LIT>' % <EOL> ( self . _request . connection . remote_addr , ) , <EOL> e ) <EOL> raise <EOL> def receive_bytes ( self , length ) : <EOL> """<STR_LIT>""" <EOL> read_bytes = [ ] <EOL> while length > <NUM_LIT:0> : <EOL> new_read_bytes = self . _read ( length ) <EOL> read_bytes . append ( new_read_bytes ) <EOL> length -= len ( new_read_bytes ) <EOL> return '<STR_LIT>' . join ( read_bytes ) <EOL> def _read_until ( self , delim_char ) : <EOL> """<STR_LIT>""" <EOL> read_bytes = [ ] <EOL> while True : <EOL> ch = self . _read ( <NUM_LIT:1> ) <EOL> if ch == delim_char : <EOL> break <EOL> read_bytes . append ( ch ) <EOL> return '<STR_LIT>' . join ( read_bytes ) </s>
