<s> from Queue import Queue <EOL> import getopt <EOL> import random <EOL> import sys <EOL> import threading <EOL> from coapthon import defines <EOL> from coapthon . client . coap import CoAP <EOL> from coapthon . client . helperclient import HelperClient <EOL> from coapthon . messages . message import Message <EOL> from coapthon . messages . request import Request <EOL> from coapthon . utils import parse_uri <EOL> client = None <EOL> def usage ( ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def client_callback ( response ) : <EOL> print "<STR_LIT>" <EOL> def client_callback_observe ( response ) : <EOL> global client <EOL> print "<STR_LIT>" <EOL> check = True <EOL> while check : <EOL> chosen = raw_input ( "<STR_LIT>" ) <EOL> if chosen != "<STR_LIT>" and not ( chosen == "<STR_LIT:n>" or chosen == "<STR_LIT:N>" or chosen == "<STR_LIT:y>" or chosen == "<STR_LIT:Y>" ) : <EOL> print "<STR_LIT>" <EOL> continue <EOL> elif chosen == "<STR_LIT:y>" or chosen == "<STR_LIT:Y>" : <EOL> while True : <EOL> rst = raw_input ( "<STR_LIT>" ) <EOL> if rst != "<STR_LIT>" and not ( rst == "<STR_LIT:n>" or rst == "<STR_LIT:N>" or rst == "<STR_LIT:y>" or rst == "<STR_LIT:Y>" ) : <EOL> print "<STR_LIT>" <EOL> continue <EOL> elif rst == "<STR_LIT>" or rst == "<STR_LIT:y>" or rst == "<STR_LIT:Y>" : <EOL> client . cancel_observing ( response , True ) <EOL> else : <EOL> client . cancel_observing ( response , False ) <EOL> check = False <EOL> break <EOL> else : <EOL> break <EOL> def main ( ) : <EOL> global client <EOL> op = None <EOL> path = None <EOL> payload = None <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> except getopt . GetoptError as err : <EOL> print str ( err ) <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> for o , a in opts : <EOL> if o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> op = a <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> path = a <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> payload = a <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> with open ( a , '<STR_LIT:r>' ) as f : <EOL> payload = f . read ( ) <EOL> elif o in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> usage ( ) <EOL> sys . exit ( ) <EOL> else : <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if op is None : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if path is None : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if not path . startswith ( "<STR_LIT>" ) : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> host , port , path = parse_uri ( path ) <EOL> client = HelperClient ( server = ( host , port ) ) <EOL> if op == "<STR_LIT:GET>" : <EOL> if path is None : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> response = client . get ( path ) <EOL> print response . pretty_print ( ) <EOL> client . stop ( ) <EOL> elif op == "<STR_LIT>" : <EOL> if path is None : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> client . observe ( path , client_callback_observe ) <EOL> elif op == "<STR_LIT>" : <EOL> if path is None : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> response = client . delete ( path ) <EOL> print response . pretty_print ( ) <EOL> client . stop ( ) <EOL> elif op == "<STR_LIT:POST>" : <EOL> if path is None : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if payload is None : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> response = client . post ( path , payload ) <EOL> print response . pretty_print ( ) <EOL> client . stop ( ) <EOL> elif op == "<STR_LIT>" : <EOL> if path is None : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if payload is None : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> response = client . put ( path , payload ) <EOL> print response . pretty_print ( ) <EOL> client . stop ( ) <EOL> elif op == "<STR_LIT>" : <EOL> response = client . discover ( ) <EOL> print response . pretty_print ( ) <EOL> client . stop ( ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
