社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

jovicbg • 5 年前 • 163 次点击  

下面有一个代码,我需要转换成Python代码,这对我来说有一些问题。 首先,我不确定缓冲区是否有意义。

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数据)之间的代码,这方面的帮助也不错,但不是主要的。缓冲区和try/catch是必须重要的。

如果有任何问题或其他我可以提供的东西,我在这里。欢迎任何形式的帮助,提前谢谢。:)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38041
 
163 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Justin Wilson
Reply   •   1 楼
Justin Wilson    6 年前

我不得不做类似的转换,但另一方面,从Python到Java。我认为缓冲区有字节的数据,所以切片是访问特定字节并设置为变量的。不好意思,我本可以发表评论的,但不能,因为我还没有50个声望。