<s> import tornado . httpserver <EOL> import tornado . ioloop <EOL> import tornado . web <EOL> import sys <EOL> from scribe import scribe <EOL> from thrift . transport import TTransport , TSocket <EOL> from thrift . protocol import TBinaryProtocol <EOL> import logging <EOL> import hmac <EOL> import hashlib <EOL> import base64 <EOL> import urllib <EOL> import socket <EOL> import traceback <EOL> import simplejson <EOL> import re <EOL> '''<STR_LIT>''' <EOL> LOGGING_SHARED_SECRET = "<STR_LIT>" <EOL> SERVER_HOSTNAME = "<STR_LIT:none>" <EOL> try : <EOL> SERVER_HOSTNAME = socket . gethostbyname ( socket . gethostname ( ) ) <EOL> except : <EOL> pass <EOL> logging . basicConfig ( level = logging . WARN ) <EOL> scribe_transport = None <EOL> scribe_client = None <EOL> SCRIBE_HOST = '<STR_LIT:127.0.0.1>' <EOL> SCRIBE_PORT = <NUM_LIT> <EOL> socket = TSocket . TSocket ( host = SCRIBE_HOST , port = int ( SCRIBE_PORT ) ) <EOL> scribe_transport = TTransport . TFramedTransport ( socket ) <EOL> protocol = TBinaryProtocol . TBinaryProtocol ( trans = scribe_transport , strictRead = False , strictWrite = False ) <EOL> scribe_client = scribe . Client ( iprot = protocol , oprot = protocol ) <EOL> class LogCatcherHandler ( tornado . web . RequestHandler ) : <EOL> def get ( self ) : <EOL> self . write ( "<STR_LIT>" + SERVER_HOSTNAME + "<STR_LIT>" ) <EOL> self . write ( "<STR_LIT>" ) <EOL> self . get_params_and_process_message ( ) <EOL> def post ( self ) : <EOL> self . write ( "<STR_LIT>" + SERVER_HOSTNAME + "<STR_LIT>" ) <EOL> self . write ( "<STR_LIT>" ) <EOL> self . get_params_and_process_message ( ) <EOL> def get_params_and_process_message ( self ) : <EOL> try : <EOL> try : <EOL> self . category = self . get_argument ( '<STR_LIT>' ) <EOL> except : <EOL> message = None <EOL> try : message = self . get_argument ( '<STR_LIT:message>' ) . encode ( "<STR_LIT:utf-8>" ) <EOL> except : pass <EOL> logging . error ( "<STR_LIT>" + str ( message ) ) <EOL> self . message = self . get_argument ( '<STR_LIT:message>' ) . encode ( "<STR_LIT:utf-8>" ) <EOL> self . hash = self . get_argument ( '<STR_LIT>' ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:+>' ) <EOL> if not self . category or not self . message or not self . hash : <EOL> self . write ( "<STR_LIT>" ) <EOL> logging . error ( "<STR_LIT>" ) <EOL> return <EOL> message_for_hash = self . message <EOL> digest = hmac . new ( LOGGING_SHARED_SECRET , message_for_hash , hashlib . sha1 ) . digest ( ) <EOL> calculated_hash = base64 . encodestring ( digest ) <EOL> hash_matches = ( self . hash [ : <NUM_LIT> ] == calculated_hash [ : <NUM_LIT> ] ) <EOL> if hash_matches : <EOL> self . write ( "<STR_LIT>" ) <EOL> forward_message ( self . category , self . message ) <EOL> else : <EOL> self . write ( "<STR_LIT>" ) <EOL> logging . warn ( "<STR_LIT>" + str ( calculated_hash ) ) <EOL> logging . warn ( "<STR_LIT>" + str ( self . hash ) ) <EOL> logging . warn ( "<STR_LIT>" + str ( hash_matches ) ) <EOL> try : <EOL> logging . warn ( "<STR_LIT>" + self . message [ : <NUM_LIT> ] ) <EOL> except : <EOL> logging . warn ( "<STR_LIT>" ) <EOL> except : <EOL> logging . error ( "<STR_LIT>" ) <EOL> traceback . print_exc ( ) <EOL> def forward_message ( category , message ) : <EOL> send_records_to_scribe ( category , message ) <EOL> def send_records_to_scribe ( category , message ) : <EOL> try : <EOL> global scribe_transport <EOL> global scribe_client <EOL> if not scribe_transport or not scribe_client : <EOL> logging . warn ( "<STR_LIT>" ) <EOL> socket = TSocket . TSocket ( host = SCRIBE_HOST , port = int ( SCRIBE_PORT ) ) <EOL> scribe_transport = TTransport . TFramedTransport ( socket ) <EOL> protocol = TBinaryProtocol . TBinaryProtocol ( trans = scribe_transport , strictRead = False , strictWrite = False ) <EOL> scribe_client = scribe . Client ( iprot = protocol , oprot = protocol ) <EOL> scribe_transport . open ( ) <EOL> lines = message . split ( "<STR_LIT:\n>" ) <EOL> log_entries = [ ] <EOL> for line in lines : <EOL> log_entry = scribe . LogEntry ( category = category , message = line ) <EOL> log_entries . append ( log_entry ) <EOL> result = scribe_client . Log ( messages = log_entries ) <EOL> scribe_transport . close ( ) <EOL> if result == scribe . ResultCode . OK : <EOL> pass <EOL> elif result == scribe . ResultCode . TRY_LATER : <EOL> logging . error ( "<STR_LIT>" + str ( result ) ) <EOL> else : <EOL> logging . error ( "<STR_LIT>" + str ( result ) ) <EOL> except : <EOL> logging . error ( "<STR_LIT>" ) <EOL> traceback . print_exc ( ) <EOL> class HeartbeatHandler ( tornado . web . RequestHandler ) : <EOL> def get ( self ) : <EOL> self . write ( "<STR_LIT>" ) <EOL> application = tornado . web . Application ( [ <EOL> ( r"<STR_LIT>" , LogCatcherHandler ) , <EOL> ( r"<STR_LIT>" , HeartbeatHandler ) , <EOL> ] ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> http_server = tornado . httpserver . HTTPServer ( application ) <EOL> http_server . listen ( <NUM_LIT> ) <EOL> tornado . ioloop . IOLoop . instance ( ) . start ( ) </s>
