<s> import os , sys , thread , socket <EOL> BACKLOG = <NUM_LIT:50> <EOL> MAX_DATA_RECV = <NUM_LIT> <EOL> DEBUG = True <EOL> BLOCKED = [ ] <EOL> def main ( ) : <EOL> if ( len ( sys . argv ) < <NUM_LIT:2> ) : <EOL> print "<STR_LIT>" <EOL> port = <NUM_LIT> <EOL> else : <EOL> port = int ( sys . argv [ <NUM_LIT:1> ] ) <EOL> host = '<STR_LIT>' <EOL> print "<STR_LIT>" , host , "<STR_LIT::>" , port <EOL> try : <EOL> s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> s . bind ( ( host , port ) ) <EOL> s . listen ( BACKLOG ) <EOL> except socket . error , ( value , message ) : <EOL> if s : <EOL> s . close ( ) <EOL> print "<STR_LIT>" , message <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> while <NUM_LIT:1> : <EOL> conn , client_addr = s . accept ( ) <EOL> thread . start_new_thread ( proxy_thread , ( conn , client_addr ) ) <EOL> s . close ( ) <EOL> def printout ( type , request , address ) : <EOL> if "<STR_LIT>" in type or "<STR_LIT>" in type : <EOL> colornum = <NUM_LIT> <EOL> elif "<STR_LIT>" in type : <EOL> colornum = <NUM_LIT> <EOL> elif "<STR_LIT>" in type : <EOL> colornum = <NUM_LIT> <EOL> print "<STR_LIT>" , colornum , "<STR_LIT:m>" , address [ <NUM_LIT:0> ] , "<STR_LIT:\t>" , type , "<STR_LIT:\t>" , request , "<STR_LIT>" <EOL> def proxy_thread ( conn , client_addr ) : <EOL> request = conn . recv ( MAX_DATA_RECV ) <EOL> first_line = request . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> url = first_line . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:1> ] <EOL> for i in range ( <NUM_LIT:0> , len ( BLOCKED ) ) : <EOL> if BLOCKED [ i ] in url : <EOL> printout ( "<STR_LIT>" , first_line , client_addr ) <EOL> conn . close ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> printout ( "<STR_LIT>" , first_line , client_addr ) <EOL> http_pos = url . find ( "<STR_LIT>" ) <EOL> if ( http_pos == - <NUM_LIT:1> ) : <EOL> temp = url <EOL> else : <EOL> temp = url [ ( http_pos + <NUM_LIT:3> ) : ] <EOL> port_pos = temp . find ( "<STR_LIT::>" ) <EOL> webserver_pos = temp . find ( "<STR_LIT:/>" ) <EOL> if webserver_pos == - <NUM_LIT:1> : <EOL> webserver_pos = len ( temp ) <EOL> webserver = "<STR_LIT>" <EOL> port = - <NUM_LIT:1> <EOL> if ( port_pos == - <NUM_LIT:1> or webserver_pos < port_pos ) : <EOL> port = <NUM_LIT> <EOL> webserver = temp [ : webserver_pos ] <EOL> else : <EOL> port = int ( ( temp [ ( port_pos + <NUM_LIT:1> ) : ] ) [ : webserver_pos - port_pos - <NUM_LIT:1> ] ) <EOL> webserver = temp [ : port_pos ] <EOL> try : <EOL> s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> s . connect ( ( webserver , port ) ) <EOL> s . send ( request ) <EOL> while <NUM_LIT:1> : <EOL> data = s . recv ( MAX_DATA_RECV ) <EOL> if ( len ( data ) > <NUM_LIT:0> ) : <EOL> conn . send ( data ) <EOL> else : <EOL> break <EOL> s . close ( ) <EOL> conn . close ( ) <EOL> except socket . error , ( value , message ) : <EOL> if s : <EOL> s . close ( ) <EOL> if conn : <EOL> conn . close ( ) <EOL> printout ( "<STR_LIT>" , first_line , client_addr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
