Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving response from network and print with Python
    text
    copied!<p>I have a problem. I've written this part of code in Python (Tornado web server):</p> <pre><code>if command == 'RESTARTNWK': op_group = "A3" op_code = "E0" netid = hextransform(int(nid), 16) packet_meta = "*%s;%s;%s;#" pkt_len = hextransform(0, 2) packet = packet_meta % (op_group, op_code, pkt_len) packet = packet.upper() op_group_hex=0xA3 op_code_hex=0xE0 cmdjson = packet2json(op_group_hex,op_code_hex, packet) mynet_type="ztc" print("\t\t " + packet + "\n") #TODO : -write command into db ts = datetime.datetime.now().isoformat() mynet_type ="ztc" self.lock_tables("write", ['confcommands']) self.db.execute("INSERT INTO confcommands (network_id, ntype, timestamp, command) \ VALUES (%s,%s,%s,%s)", nid, mynet_type, ts, cmdjson) self.unlock_tables(); # TODO: - open the /tmp/iztc file in append mode cmdfile = open('/tmp/iztc', 'a') # - acquire a lock "only for the DB case, it's easier" # - write the packet cmdfile.write(netid + "\t"+ mynet_type + "\t"+ ts + "\t"+ cmdjson +"\n"); # - release the lock "only for the DB case, it's easier" # - close the file cmdfile.close() if command == 'RESTARTNWK': opcodegroupr = "A4" opcoder = "E0" #Code for retrieving the MAC address of the node como_url = "".join(['http://', options.como_address, ':', options.como_port, '/', ztc_config, '?netid=', netid, '&amp;opcode_group=', opcodegroupr, '&amp;opcode=', opcoder, '&amp;start=-5m&amp;end=-1s']) http_client = AsyncHTTPClient() response = yield tornado.gen.Task(http_client.fetch, como_url) ret = {} if response.error: ret['error'] = 'Error while retrieving unregistered sensors' else: for line in response.body.split("\n"): if line != "": value = int(line.split(" ")[6]) ret['response'] = value self.write(tornado.escape.json_encode(ret)) self.finish() </code></pre> <p>In this code I receive the restart network command from the user. After some settings, I write the relative command in a db table named confcommands. The server will read this command and will send to the specified network the restart signal.</p> <p>After this, if all it's ok, the network resend me the response. I read this response with a http request to my server (como), and wait for the asynchronous response.</p> <p>Where the response is written by the network, I have to find this in the packet. The value response is the sixth element. Other information of the packet are the opgroup and opcode, the network from which is the response, and other informations. </p> <p>Then I write the response for the user.</p> <p>I don't know if this code is right... can work this? The structure seems to me right....</p> <p>Thank you all for any suggestions!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload