下面有一个我需要转换为python代码的代码,对我来说有一些问题。 首先不确定我是否理解Buffer。
var dgram = require("dgram");
var async = require("async");
var util = require("util");
var server = dgram.createSocket('dod3');
var domestic = require('./utils/domestic');
server.on("message", function (mess_data, remote) {
var buf = new Buffer(mess_data, 'hex');
if(buf[22] === 2) {
return;
}
var log_date = new Date().toISOString();
try {
var adress_m = buf.slice(16, 22).toString('hex').toUpperCase();
var status_id = buf.readUInt32BE(4);
var usage = buf.slice(1,2).toString('hex');
//var delta_time = buf.readUInt32BE(8);
var time = parseInt((new Date().getTime() / 1000), 10);
}
catch(ex) {
console.error(util.format('%s PARSE 500 \"%s\"', log_date, ex));
}
async.waterfall([
function(callback){
client.hgetall(ams_id, function (error, domestic){
if(domestic) {
callback(null, domestic["domestic_id"]);
}
else {
console.error(util.format('%s 404 \" No data found for %s | %s\"', log_date, status_id, error));
callback(null, null)
}
})
},
function(domestic_id, callback){
if(domestic_id) {
callback(null, domestic_id);
}
else {
domestic.get_domestic_id_by_ams(ams_id, function(result){
callback(null, result);
});
}
}
], function (error, domestic_id) {
if(domestic_id == null) {
return;
}
if(ams_id.toString().length > 4) {
ams_id = ams_id - 16777216;
}
var data = {
"user": {
"enter" : false
},
"device" : {
"type" : "fev",
"mac": adress_m
},
"event" : {
"origin" : "call",
"timestamp": time,
"type": "zap",
"product-type" : "domestic",
"channel": {
"id" : domestic_id,
"ams-id": ams_id
},
"content": {
"usage": usage
}
}
};
我的python代码:
def parse_jspy(mess_data):
buf = (hashlib.sha256(bytearray.fromhex(mess_data)).hexdigest())
buf = int(buf, 16)
buf_check = str(buf)
if buf_check[22] == 2:
pass
datetime_now = datetime.now()
log_date = datetime_now.isoformat()
adress_m = buf_check[16:22]
status_id = buf_check[4:]
usage = buf_check[1:2]
time_a = int(time())
dict_test = {
"user": {
"enter" : 'false'
},
"device" : {
"type" : "net",
"adress_m": adress_m
},
"event" : {
"origin" : "flex",
"timestamp": time_a,
"type": "relevant",
"product-type" : "info",
"channel": {
"id" : domestic_id,
"status_id": status_id
},
"content": {
"usage": usage
}
}
};
print dict_test
test_data = "02 01 01 dc 01 00 02 02 00 01 9a ba 02 01 9a bb 01 04 30 53 23 11 01"
主要问题是我无法真正理解javascript代码并且不确定此缓冲函数,因此导致以下函数出现问题。
另外,我无法理解“ async.waterfall”和json(var数据)之间的代码,对此的帮助也很好,但不是主要的。缓冲和尝试/捕获是必须重要的。
如果有任何问题或我可以提供的其他东西,我在这里。欢迎任何帮助,在此先感谢。 :)
答案 0 :(得分:1)
我不得不做类似的转换,但是从python到java。我认为缓冲区具有字节数据,因此切片是访问特定的字节并设置为变量。抱歉,我刚才已经发表评论了,但不能,因为还没有50名。