<s> import json <EOL> import logging <EOL> from email import message_from_string <EOL> import random <EOL> from StringIO import StringIO <EOL> from threading import Thread <EOL> import time <EOL> import urlparse <EOL> from flask import Flask , request , Response , g <EOL> from httpbin . helpers import get_dict , status_code <EOL> from twisted . internet import protocol , reactor <EOL> from twisted . web . server import Site <EOL> from twisted . web . wsgi import WSGIResource <EOL> from werkzeug . routing import Rule <EOL> from werkzeug . http import parse_accept_header <EOL> from . morse import morsedict <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> logger . setLevel ( logging . INFO ) <EOL> __version__ = '<STR_LIT>' <EOL> SERVER_HEADER = '<STR_LIT>' . format ( version = __version__ ) <EOL> BASE_PORT = <NUM_LIT> <EOL> class HammsSite ( Site ) : <EOL> def getResourceFor ( self , request ) : <EOL> request . setHeader ( '<STR_LIT>' , SERVER_HEADER ) <EOL> return Site . getResourceFor ( self , request ) <EOL> class HammsServer ( object ) : <EOL> """<STR_LIT>""" <EOL> def start ( self , beginning_port = BASE_PORT ) : <EOL> self . beginning_port = beginning_port <EOL> self . retry_cache = { } <EOL> listen ( reactor , base_port = self . beginning_port , retry_cache = self . retry_cache ) <EOL> if not reactor . running : <EOL> self . t = Thread ( target = reactor . run , args = ( False , ) ) <EOL> self . t . daemon = True <EOL> self . t . start ( ) <EOL> def stop ( self ) : <EOL> reactor . stop ( ) <EOL> def listen ( _reactor , base_port = BASE_PORT , retry_cache = None ) : <EOL> retry_cache = retry_cache or { } <EOL> retries_app = create_retries_app ( retry_cache ) <EOL> sleep_resource = WSGIResource ( reactor , reactor . getThreadPool ( ) , sleep_app ) <EOL> sleep_site = HammsSite ( sleep_resource ) <EOL> status_resource = WSGIResource ( reactor , reactor . getThreadPool ( ) , status_app ) <EOL> status_site = HammsSite ( status_resource ) <EOL> large_header_resource = WSGIResource ( reactor , reactor . getThreadPool ( ) , <EOL> large_header_app ) <EOL> large_header_site = HammsSite ( large_header_resource ) <EOL> retries_resource = WSGIResource ( reactor , reactor . getThreadPool ( ) , retries_app ) <EOL> retries_site = HammsSite ( retries_resource ) <EOL> unparseable_resource = WSGIResource ( reactor , reactor . getThreadPool ( ) , <EOL> unparseable_app ) <EOL> unparseable_site = HammsSite ( unparseable_resource ) <EOL> toolong_content_resource = WSGIResource ( reactor , reactor . getThreadPool ( ) , <EOL> toolong_content_app ) <EOL> toolong_content_site = HammsSite ( toolong_content_resource ) <EOL> _reactor . listenTCP ( base_port + ListenForeverServer . PORT , ListenForeverFactory ( ) ) <EOL> _reactor . listenTCP ( base_port + EmptyStringTerminateImmediatelyServer . PORT , <EOL> EmptyStringTerminateImmediatelyFactory ( ) ) <EOL> _reactor . listenTCP ( base_port + EmptyStringTerminateOnReceiveServer . PORT , <EOL> EmptyStringTerminateOnReceiveFactory ( ) ) <EOL> _reactor . listenTCP ( base_port + MalformedStringTerminateImmediatelyServer . PORT , <EOL> MalformedStringTerminateImmediatelyFactory ( ) ) <EOL> _reactor . listenTCP ( base_port + MalformedStringTerminateOnReceiveServer . PORT , <EOL> MalformedStringTerminateOnReceiveFactory ( ) ) <EOL> _reactor . listenTCP ( base_port + FiveSecondByteResponseServer . PORT , <EOL> FiveSecondByteResponseFactory ( ) ) <EOL> _reactor . listenTCP ( base_port + ThirtySecondByteResponseServer . PORT , <EOL> ThirtySecondByteResponseFactory ( ) ) <EOL> _reactor . listenTCP ( base_port + sleep_app . PORT , sleep_site ) <EOL> _reactor . listenTCP ( base_port + status_app . PORT , status_site ) <EOL> _reactor . listenTCP ( base_port + SendDataPastContentLengthServer . PORT , <EOL> SendDataPastContentLengthFactory ( ) ) <EOL> _reactor . listenTCP ( base_port + large_header_app . PORT , large_header_site ) <EOL> _reactor . listenTCP ( base_port + retries_app . PORT , retries_site ) <EOL> _reactor . listenTCP ( base_port + DropRandomRequestsServer . PORT , <EOL> DropRandomRequestsFactory ( ) ) <EOL> _reactor . listenTCP ( base_port + unparseable_app . PORT , unparseable_site ) <EOL> _reactor . listenTCP ( base_port + IncompleteResponseServer . PORT , <EOL> IncompleteResponseFactory ( ) ) <EOL> _reactor . listenTCP ( base_port + toolong_content_app . PORT , toolong_content_site ) <EOL> def get_remote_host ( transport ) : <EOL> try : <EOL> peer = transport . getPeer ( ) <EOL> return peer . host <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> def get_port ( transport ) : <EOL> try : <EOL> return transport . getHost ( ) . port <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> def get_header ( header_name , data ) : <EOL> try : <EOL> rline , raw_headers = data . split ( '<STR_LIT:\r\n>' , <NUM_LIT:1> ) <EOL> headers = message_from_string ( raw_headers ) <EOL> return headers . get ( header_name , "<STR_LIT>" ) <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> def _log_t ( transport , data , status = None ) : <EOL> ipaddr = get_remote_host ( transport ) <EOL> port = get_port ( transport ) <EOL> ua = get_header ( '<STR_LIT>' , data ) <EOL> return _log ( ipaddr , port , data , status = status , ua = ua ) <EOL> def _log ( ipaddr , port , data , status = None , ua = "<STR_LIT>" ) : <EOL> try : <EOL> topline = data . split ( '<STR_LIT:\r\n>' ) [ <NUM_LIT:0> ] <EOL> return "<STR_LIT>" . format ( <EOL> ipaddr = ipaddr , port = port , topline = topline , ua = ua , status = status or "<STR_LIT:->" ) <EOL> except Exception : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" <EOL> class ListenForeverServer ( protocol . Protocol ) : <EOL> PORT = <NUM_LIT:1> <EOL> def dataReceived ( self , data ) : <EOL> logger . info ( _log_t ( self . transport , data ) ) <EOL> class ListenForeverFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return ListenForeverServer ( ) <EOL> class EmptyStringTerminateImmediatelyServer ( protocol . Protocol ) : <EOL> PORT = <NUM_LIT:2> <EOL> def dataReceived ( self , data ) : <EOL> logger . info ( _log_t ( self . transport , data ) ) <EOL> def connectionMade ( self ) : <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> self . transport . loseConnection ( ) <EOL> class EmptyStringTerminateImmediatelyFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return EmptyStringTerminateImmediatelyServer ( ) <EOL> class EmptyStringTerminateOnReceiveServer ( protocol . Protocol ) : <EOL> PORT = <NUM_LIT:3> <EOL> def dataReceived ( self , data ) : <EOL> logger . info ( _log_t ( self . transport , data ) ) <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> self . transport . loseConnection ( ) <EOL> class EmptyStringTerminateOnReceiveFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return EmptyStringTerminateOnReceiveServer ( ) <EOL> class MalformedStringTerminateImmediatelyServer ( protocol . Protocol ) : <EOL> PORT = <NUM_LIT:4> <EOL> def dataReceived ( self , data ) : <EOL> logger . info ( _log_t ( self . transport , data ) ) <EOL> def connectionMade ( self ) : <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> self . transport . loseConnection ( ) <EOL> class MalformedStringTerminateImmediatelyFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return MalformedStringTerminateImmediatelyServer ( ) <EOL> class MalformedStringTerminateOnReceiveServer ( protocol . Protocol ) : <EOL> PORT = <NUM_LIT:5> <EOL> def dataReceived ( self , data ) : <EOL> logger . info ( _log_t ( self . transport , data ) ) <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> self . transport . loseConnection ( ) <EOL> class MalformedStringTerminateOnReceiveFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return MalformedStringTerminateOnReceiveServer ( ) <EOL> empty_response = ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( hdr = SERVER_HEADER ) ) <EOL> class FiveSecondByteResponseServer ( protocol . Protocol ) : <EOL> PORT = <NUM_LIT:6> <EOL> def _send_byte ( self , byte ) : <EOL> self . transport . write ( byte ) <EOL> def dataReceived ( self , data ) : <EOL> try : <EOL> timer = <NUM_LIT:5> <EOL> for byte in empty_response : <EOL> reactor . callLater ( timer , self . _send_byte , byte ) <EOL> timer += <NUM_LIT:5> <EOL> reactor . callLater ( timer , self . transport . loseConnection ) <EOL> logger . info ( _log_t ( self . transport , data , status = <NUM_LIT> ) ) <EOL> except Exception : <EOL> logger . info ( _log_t ( self . transport , data ) ) <EOL> class FiveSecondByteResponseFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return FiveSecondByteResponseServer ( ) <EOL> class ThirtySecondByteResponseServer ( protocol . Protocol ) : <EOL> PORT = <NUM_LIT:7> <EOL> def _send_byte ( self , byte ) : <EOL> self . transport . write ( byte ) <EOL> def dataReceived ( self , data ) : <EOL> try : <EOL> timer = <NUM_LIT:30> <EOL> for byte in empty_response : <EOL> reactor . callLater ( timer , self . _send_byte , byte ) <EOL> timer += <NUM_LIT:30> <EOL> reactor . callLater ( timer , self . transport . loseConnection ) <EOL> logger . info ( _log_t ( self . transport , data , status = <NUM_LIT> ) ) <EOL> except Exception : <EOL> logger . info ( _log_t ( self . transport , data ) ) <EOL> class ThirtySecondByteResponseFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return ThirtySecondByteResponseServer ( ) <EOL> class SendDataPastContentLengthServer ( protocol . Protocol ) : <EOL> PORT = <NUM_LIT:10> <EOL> def dataReceived ( self , data ) : <EOL> logger . info ( _log_t ( self . transport , data , status = <NUM_LIT:200> ) ) <EOL> def connectionMade ( self ) : <EOL> self . transport . write ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( server = SERVER_HEADER , <EOL> body = '<STR_LIT:a>' * <NUM_LIT> * <NUM_LIT> ) ) <EOL> self . transport . loseConnection ( ) <EOL> class SendDataPastContentLengthFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return SendDataPastContentLengthServer ( ) <EOL> def success_response ( content_type , response ) : <EOL> return ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( ctype = content_type , server = SERVER_HEADER , <EOL> response = response ) ) <EOL> class DropRandomRequestsServer ( protocol . Protocol ) : <EOL> PORT = <NUM_LIT> <EOL> def dataReceived ( self , data ) : <EOL> body = data . split ( '<STR_LIT:\r\n>' ) <EOL> try : <EOL> method , url , http_vsn = body [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+0020>' ) <EOL> except Exception : <EOL> logger . info ( _log_t ( self . transport , data ) ) <EOL> self . transport . loseConnection ( ) <EOL> o = urlparse . urlparse ( url ) <EOL> query = urlparse . parse_qs ( o . query ) <EOL> if '<STR_LIT>' in query : <EOL> failrate = query [ '<STR_LIT>' ] . pop ( ) <EOL> else : <EOL> failrate = <NUM_LIT> <EOL> if random . random ( ) >= float ( failrate ) : <EOL> logger . info ( _log_t ( self . transport , data , status = <NUM_LIT:200> ) ) <EOL> self . transport . write ( <EOL> success_response ( '<STR_LIT:application/json>' , '<STR_LIT>' ) ) <EOL> else : <EOL> logger . info ( _log_t ( self . transport , data ) ) <EOL> self . transport . loseConnection ( ) <EOL> class DropRandomRequestsFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return DropRandomRequestsServer ( ) <EOL> def write_incomplete_response ( transport , content_type , body ) : <EOL> transport . write ( '<STR_LIT>' . format ( ctype = content_type ) ) <EOL> transport . write ( '<STR_LIT>' . format ( <EOL> length = len ( body ) + <NUM_LIT> ) ) <EOL> transport . write ( '<STR_LIT>' . format ( body = body ) ) <EOL> transport . loseConnection ( ) <EOL> INCOMPLETE_JSON = '<STR_LIT>' <EOL> INCOMPLETE_XML = '<STR_LIT>' <EOL> INCOMPLETE_PLAIN = '<STR_LIT>' <EOL> INCOMPLETE_HTML = '<STR_LIT>' <EOL> class IncompleteResponseServer ( protocol . Protocol ) : <EOL> PORT = <NUM_LIT:16> <EOL> def dataReceived ( self , data ) : <EOL> accept_header_value = get_header ( '<STR_LIT>' , data ) <EOL> accept_cls = parse_accept_header ( accept_header_value ) <EOL> self . transport . write ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' == accept_cls . best : <EOL> write_incomplete_response ( self . transport , '<STR_LIT>' , <EOL> INCOMPLETE_HTML ) <EOL> elif '<STR_LIT>' == accept_cls . best : <EOL> write_incomplete_response ( self . transport , '<STR_LIT>' , <EOL> INCOMPLETE_PLAIN ) <EOL> elif '<STR_LIT>' == accept_cls . best : <EOL> write_incomplete_response ( self . transport , '<STR_LIT>' , <EOL> INCOMPLETE_XML ) <EOL> else : <EOL> write_incomplete_response ( self . transport , '<STR_LIT:application/json>' , <EOL> INCOMPLETE_JSON ) <EOL> class IncompleteResponseFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return IncompleteResponseServer ( ) <EOL> sleep_app = Flask ( __name__ ) <EOL> sleep_app . PORT = <NUM_LIT:8> <EOL> status_app = Flask ( __name__ ) <EOL> status_app . PORT = <NUM_LIT:9> <EOL> large_header_app = Flask ( __name__ ) <EOL> large_header_app . PORT = <NUM_LIT:11> <EOL> unparseable_app = Flask ( __name__ ) <EOL> unparseable_app . PORT = <NUM_LIT> <EOL> toolong_content_app = Flask ( __name__ ) <EOL> toolong_content_app . PORT = <NUM_LIT:15> <EOL> def create_retries_app ( cache ) : <EOL> retries_app = Flask ( __name__ ) <EOL> retries_app . PORT = <NUM_LIT:12> <EOL> retries_app . cache = cache <EOL> retries_app . url_map . add ( Rule ( '<STR_LIT:/>' , endpoint = '<STR_LIT:index>' ) ) <EOL> @ retries_app . endpoint ( "<STR_LIT:index>" ) <EOL> def check_retries ( ) : <EOL> json_hdr = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> key = request . args . get ( '<STR_LIT:key>' , '<STR_LIT:default>' ) <EOL> tries = request . args . get ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> try : <EOL> tries = int ( tries ) <EOL> except Exception : <EOL> return Response ( status = <NUM_LIT> , headers = json_hdr , response = json . dumps ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT:success>' : False , <EOL> } ) ) <EOL> if key in retries_app . cache : <EOL> retries_app . cache [ key ] -= <NUM_LIT:1> <EOL> else : <EOL> retries_app . cache [ key ] = int ( tries ) - <NUM_LIT:1> <EOL> if retries_app . cache [ key ] <= <NUM_LIT:0> : <EOL> data = { <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT>' : retries_app . cache [ key ] , <EOL> '<STR_LIT:success>' : True <EOL> } <EOL> return Response ( response = json . dumps ( data ) , status = <NUM_LIT:200> , <EOL> headers = json_hdr ) <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> time_p = '<STR_LIT:time>' if retries_app . cache [ key ] == <NUM_LIT:1> else '<STR_LIT>' <EOL> content = { <EOL> '<STR_LIT:error>' : msg . format ( retry_times = retries_app . cache [ key ] , time_p = time_p ) , <EOL> '<STR_LIT>' : retries_app . cache [ key ] , <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT:success>' : False , <EOL> } <EOL> return Response ( response = json . dumps ( content ) , status = <NUM_LIT> , <EOL> headers = json_hdr ) <EOL> @ retries_app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def reset ( ) : <EOL> key = request . values . get ( '<STR_LIT:key>' , '<STR_LIT:default>' ) <EOL> tries = request . values . get ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> try : <EOL> tries = int ( tries ) <EOL> except Exception : <EOL> return Response ( status = <NUM_LIT> , headers = json_hdr , response = json . dumps ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT:success>' : False , <EOL> } ) ) <EOL> retries_app . cache [ key ] = tries <EOL> content = { <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT>' : tries , <EOL> '<STR_LIT:success>' : True , <EOL> } <EOL> return Response ( response = json . dumps ( content ) , status = <NUM_LIT:200> , <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> @ retries_app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:GET>' ] ) <EOL> def counter ( ) : <EOL> content = { '<STR_LIT>' : retries_app . cache , '<STR_LIT:success>' : True } <EOL> return Response ( response = json . dumps ( content ) , status = <NUM_LIT:200> , <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> @ retries_app . after_request <EOL> def retries_header ( resp ) : <EOL> _log_flask ( resp . status_code ) <EOL> resp . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return resp <EOL> return retries_app <EOL> @ sleep_app . route ( "<STR_LIT:/>" ) <EOL> def sleep ( ) : <EOL> n = request . values . get ( '<STR_LIT>' , <NUM_LIT:5> ) <EOL> time . sleep ( float ( n ) ) <EOL> hdrs = get_dict ( '<STR_LIT>' ) <EOL> return Response ( response = json . dumps ( hdrs ) , status = <NUM_LIT:200> , <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> @ status_app . route ( "<STR_LIT:/>" ) <EOL> def status ( ) : <EOL> n = request . values . get ( '<STR_LIT:status>' , <NUM_LIT:200> ) <EOL> return status_code ( int ( n ) ) <EOL> @ large_header_app . route ( "<STR_LIT:/>" ) <EOL> def large_header ( ) : <EOL> n = request . values . get ( '<STR_LIT:size>' , <NUM_LIT> * <NUM_LIT> ) <EOL> req_headers = get_dict ( '<STR_LIT>' ) <EOL> resp_headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT:a>' * int ( n ) <EOL> } <EOL> return Response ( response = json . dumps ( req_headers ) , status = <NUM_LIT:200> , <EOL> headers = resp_headers ) <EOL> @ unparseable_app . route ( "<STR_LIT:/>" ) <EOL> def unparseable ( ) : <EOL> def _morse ( ) : <EOL> hdr = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> message = "<STR_LIT>" . join ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) + "<STR_LIT>" <EOL> morse_message = StringIO ( ) <EOL> for i , letter in enumerate ( message ) : <EOL> morse_message . write ( morsedict [ letter ] ) <EOL> return Response ( response = morse_message . getvalue ( ) , headers = hdr ) <EOL> if '<STR_LIT>' not in request . accept_mimetypes : <EOL> return _morse ( ) <EOL> elif not request . accept_mimetypes . accept_json : <EOL> hdr = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> resp = { <EOL> '<STR_LIT:status>' : <NUM_LIT:200> , <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> } <EOL> return Response ( response = json . dumps ( resp ) , headers = hdr ) <EOL> elif not request . accept_mimetypes . accept_html : <EOL> hdr = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> return Response ( response = "<STR_LIT>" , headers = hdr ) <EOL> elif '<STR_LIT>' not in request . accept_mimetypes : <EOL> hdr = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> return Response ( response = "<STR_LIT>" , headers = hdr ) <EOL> else : <EOL> return _morse ( ) <EOL> @ toolong_content_app . route ( "<STR_LIT:/>" ) <EOL> def toolong ( ) : <EOL> r = Response ( ) <EOL> r . automatically_set_content_length = False <EOL> r . headers [ '<STR_LIT>' ] = <NUM_LIT> <EOL> if ( request . accept_mimetypes . best == '<STR_LIT:application/json>' or <EOL> request . accept_mimetypes . best == '<STR_LIT>' ) : <EOL> r . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> r . set_data ( INCOMPLETE_JSON ) <EOL> elif request . accept_mimetypes . best == '<STR_LIT>' : <EOL> r . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> r . set_data ( INCOMPLETE_HTML ) <EOL> elif request . accept_mimetypes . best == '<STR_LIT>' : <EOL> r . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> r . set_data ( INCOMPLETE_PLAIN ) <EOL> elif ( request . accept_mimetypes . best == '<STR_LIT>' or <EOL> request . accept_mimetypes . best == '<STR_LIT>' ) : <EOL> r . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> r . set_data ( INCOMPLETE_XML ) <EOL> else : <EOL> r . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> r . set_data ( INCOMPLETE_JSON ) <EOL> return r <EOL> def _get_port_from_url ( url ) : <EOL> urlo = urlparse . urlparse ( url ) <EOL> try : <EOL> host , port = urlo . netloc . split ( '<STR_LIT::>' ) <EOL> return port <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> def _log_flask ( status ) : <EOL> port = _get_port_from_url ( request . url ) <EOL> url_line = "<STR_LIT>" . format ( <EOL> method = request . method . upper ( ) , url = request . full_path ) <EOL> ua = request . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> logger . info ( _log ( request . remote_addr , port , url_line , status , ua = ua ) ) <EOL> @ sleep_app . after_request <EOL> def log_sleep ( resp ) : <EOL> _log_flask ( resp . status_code ) <EOL> return resp <EOL> @ status_app . after_request <EOL> def log_status ( resp ) : <EOL> _log_flask ( resp . status_code ) <EOL> return resp <EOL> @ large_header_app . after_request <EOL> def log_large_header ( resp ) : <EOL> _log_flask ( resp . status_code ) <EOL> return resp <EOL> def main ( port = BASE_PORT ) : <EOL> logging . basicConfig ( ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> listen ( reactor , port ) <EOL> reactor . run ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
