<s> from six . moves . SimpleHTTPServer import SimpleHTTPRequestHandler <EOL> from six . moves . socketserver import TCPServer <EOL> import logging <EOL> import os <EOL> import threading <EOL> import json <EOL> from weblib . encoding import make_str <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> BASE_DIR = os . path . dirname ( os . path . dirname ( os . path . realpath ( __file__ ) ) ) <EOL> class ApiHandler ( SimpleHTTPRequestHandler ) : <EOL> def do_GET ( self ) : <EOL> if self . path == '<STR_LIT:/>' : <EOL> self . home ( ) <EOL> elif self . path == '<STR_LIT>' : <EOL> self . api_info ( ) <EOL> elif self . path == '<STR_LIT>' : <EOL> self . api_stop ( ) <EOL> else : <EOL> self . not_found ( ) <EOL> def response ( self , code = <NUM_LIT:200> , content = b'<STR_LIT>' , <EOL> content_type = '<STR_LIT>' ) : <EOL> self . send_response ( code ) <EOL> self . send_header ( '<STR_LIT>' , content_type ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( content ) <EOL> def not_found ( self ) : <EOL> self . response ( <NUM_LIT> ) <EOL> def api_info ( self ) : <EOL> info = { <EOL> '<STR_LIT>' : self . spider . stat . counters , <EOL> '<STR_LIT>' : dict ( ( x , len ( y ) ) for ( x , y ) <EOL> in self . spider . stat . collections . items ( ) ) , <EOL> '<STR_LIT>' : self . spider . thread_number , <EOL> '<STR_LIT>' : self . spider . parser_pool_size , <EOL> } <EOL> content = make_str ( json . dumps ( info ) ) <EOL> self . response ( content = content ) <EOL> def api_stop ( self ) : <EOL> self . response ( ) <EOL> self . spider . stop ( ) <EOL> def home ( self ) : <EOL> html_file = os . path . join ( BASE_DIR , '<STR_LIT>' ) <EOL> content = open ( html_file , '<STR_LIT:rb>' ) . read ( ) <EOL> self . response ( content = content ) <EOL> class ReuseTCPServer ( TCPServer ) : <EOL> allow_reuse_address = True <EOL> class HttpApiThread ( threading . Thread ) : <EOL> def __init__ ( self , spider , * args , ** kwargs ) : <EOL> self . spider = spider <EOL> super ( HttpApiThread , self ) . __init__ ( * args , ** kwargs ) <EOL> def run ( self ) : <EOL> ApiHandler . spider = self . spider <EOL> self . server = ReuseTCPServer ( ( "<STR_LIT>" , self . spider . http_api_port ) , <EOL> ApiHandler ) <EOL> logging . debug ( '<STR_LIT>' <EOL> % self . spider . http_api_port ) <EOL> self . server . serve_forever ( ) </s>
