<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from twisted . internet import reactor <EOL> from twisted . internet . endpoints import connectProtocol , SSL4ClientEndpoint <EOL> from twisted . internet . protocol import Protocol <EOL> from twisted . internet . ssl import optionsForClientTLS <EOL> from hyperframe . frame import SettingsFrame <EOL> from h2 . connection import H2Connection <EOL> from h2 . events import ( <EOL> ResponseReceived , DataReceived , RemoteSettingsChanged , StreamEnded , <EOL> StreamReset , SettingsAcknowledged , <EOL> ) <EOL> AUTHORITY = u'<STR_LIT>' <EOL> PATH = '<STR_LIT:/>' <EOL> SIZE = <NUM_LIT> <EOL> class H2Protocol ( Protocol ) : <EOL> def __init__ ( self ) : <EOL> self . conn = H2Connection ( ) <EOL> self . known_proto = None <EOL> self . request_made = False <EOL> def connectionMade ( self ) : <EOL> self . conn . initiate_connection ( ) <EOL> self . conn . update_settings ( { SettingsFrame . HEADER_TABLE_SIZE : SIZE } ) <EOL> self . transport . write ( self . conn . data_to_send ( ) ) <EOL> def dataReceived ( self , data ) : <EOL> if not self . known_proto : <EOL> self . known_proto = self . transport . getNextProtocol ( ) <EOL> assert self . known_proto == b'<STR_LIT>' <EOL> events = self . conn . receive_data ( data ) <EOL> for event in events : <EOL> if isinstance ( event , ResponseReceived ) : <EOL> self . handleResponse ( event . headers , event . stream_id ) <EOL> elif isinstance ( event , DataReceived ) : <EOL> self . handleData ( event . data , event . stream_id ) <EOL> elif isinstance ( event , StreamEnded ) : <EOL> self . endStream ( event . stream_id ) <EOL> elif isinstance ( event , SettingsAcknowledged ) : <EOL> self . settingsAcked ( event ) <EOL> elif isinstance ( event , StreamReset ) : <EOL> reactor . stop ( ) <EOL> raise RuntimeError ( "<STR_LIT>" % event . error_code ) <EOL> else : <EOL> print ( event ) <EOL> data = self . conn . data_to_send ( ) <EOL> if data : <EOL> self . transport . write ( data ) <EOL> def settingsAcked ( self , event ) : <EOL> if not self . request_made : <EOL> self . sendRequest ( ) <EOL> def handleResponse ( self , response_headers , stream_id ) : <EOL> for name , value in response_headers : <EOL> print ( "<STR_LIT>" % ( name , value ) ) <EOL> print ( "<STR_LIT>" ) <EOL> def handleData ( self , data , stream_id ) : <EOL> print ( data , end = '<STR_LIT>' ) <EOL> def endStream ( self , stream_id ) : <EOL> self . conn . close_connection ( ) <EOL> self . transport . write ( self . conn . data_to_send ( ) ) <EOL> self . transport . loseConnection ( ) <EOL> reactor . stop ( ) <EOL> def sendRequest ( self ) : <EOL> request_headers = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , AUTHORITY ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , PATH ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> self . conn . send_headers ( <NUM_LIT:1> , request_headers , end_stream = True ) <EOL> self . request_made = True <EOL> options = optionsForClientTLS ( <EOL> hostname = AUTHORITY , <EOL> extraCertificateOptions = { '<STR_LIT>' : [ b'<STR_LIT>' ] } , <EOL> ) <EOL> connectProtocol ( <EOL> SSL4ClientEndpoint ( reactor , AUTHORITY , <NUM_LIT> , options ) , <EOL> H2Protocol ( ) <EOL> ) <EOL> reactor . run ( ) </s>
