javascript - 将 JavaScript 转换为 Python 代码时出现十六进制问题

标签 javascript python node.js hex buffer

我有下面的代码需要转换为 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 数据)之间的代码,对此提供帮助也很好,但不是主要的。 Buffer和try/catch是必须重要的。

如果有任何问题或我可以额外提供的信息,我就在这里。欢迎任何形式的帮助,提前致谢。 :)

最佳答案

我必须进行类似的转换,但方式不同,从 python 到 java。我认为缓冲区有数据字节,因此切片可以访问特定字节并设置为变量。抱歉,我本来想发表评论,但不能,因为还没有 50 点声誉。

关于javascript - 将 JavaScript 转换为 Python 代码时出现十六进制问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52191396/

相关文章:

javascript 和 CSS 动画冲突 - 翻译不起作用,不透明度起作用

javascript - 计算的可观察量不更新值

python - 如何使用urllib来填写表单和收集数据?

node.js - 如何在我的 Nodejs/Sails.js 应用程序中实现 OAuth?

javascript - js/nodejs 使用 request.on() 和 querystring.parse() 的 POST 方法的基本初学者服务器

javascript - Three.js - 使用 PlaneGeometry 生成山丘

javascript - 数据表的列数

python - 如何删除括号内的空格?

python - 舍入到 python 字典中的键

node.js - 从 node.js 配置 VM 实例的推荐方法和工具?