<s> import sys <EOL> import time <EOL> import errno <EOL> import threading <EOL> import binwalk . core . compat <EOL> try : <EOL> import SocketServer <EOL> except ImportError : <EOL> import socketserver as SocketServer <EOL> class StatusRequestHandler ( SocketServer . BaseRequestHandler ) : <EOL> def handle ( self ) : <EOL> message_format = "<STR_LIT>" <EOL> last_status_message_len = <NUM_LIT:0> <EOL> status_message = '<STR_LIT>' <EOL> message_sent = False <EOL> self . server . binwalk . status . running = True <EOL> while True : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> try : <EOL> self . request . send ( binwalk . core . compat . str2bytes ( '<STR_LIT>' * last_status_message_len ) ) <EOL> self . request . send ( binwalk . core . compat . str2bytes ( '<STR_LIT:U+0020>' * last_status_message_len ) ) <EOL> self . request . send ( binwalk . core . compat . str2bytes ( '<STR_LIT>' * last_status_message_len ) ) <EOL> if self . server . binwalk . status . shutdown : <EOL> self . server . binwalk . status . finished = True <EOL> break <EOL> if self . server . binwalk . status . total != <NUM_LIT:0> : <EOL> percentage = ( ( float ( self . server . binwalk . status . completed ) / float ( self . server . binwalk . status . total ) ) * <NUM_LIT:100> ) <EOL> status_message = message_format % ( self . server . binwalk . status . fp . path , <EOL> percentage , <EOL> self . server . binwalk . status . completed , <EOL> self . server . binwalk . status . total ) <EOL> elif not message_sent : <EOL> status_message = "<STR_LIT>" <EOL> else : <EOL> continue <EOL> last_status_message_len = len ( status_message ) <EOL> self . request . send ( binwalk . core . compat . str2bytes ( status_message ) ) <EOL> message_sent = True <EOL> except IOError as e : <EOL> if e . errno == errno . EPIPE : <EOL> break <EOL> except Exception as e : <EOL> binwalk . core . common . debug ( '<STR_LIT>' + str ( e ) + '<STR_LIT:\n>' ) <EOL> except KeyboardInterrupt as e : <EOL> raise e <EOL> self . server . binwalk . status . running = False <EOL> return <EOL> class ThreadedStatusServer ( SocketServer . ThreadingMixIn , SocketServer . TCPServer ) : <EOL> daemon_threads = True <EOL> allow_reuse_address = True <EOL> class StatusServer ( object ) : <EOL> def __init__ ( self , port , binwalk ) : <EOL> self . server = ThreadedStatusServer ( ( '<STR_LIT:127.0.0.1>' , port ) , StatusRequestHandler ) <EOL> self . server . binwalk = binwalk <EOL> t = threading . Thread ( target = self . server . serve_forever ) <EOL> t . setDaemon ( True ) <EOL> t . start ( ) </s>
