将邮递员的预请求脚本转换为python?

时间:2019-03-07 07:04:11

标签: python python-2.7 python-requests postman postman-pre-request-script

我正在使用邮递员来测试REST呼叫。但是在执行此操作时,我使用的是邮递员的Pre-request脚本。现在,我想编写一个python代码来完成此规定。但是我不知道这个脚本。下面是邮递员脚本。

var uuid = function() {};
uuid.v4 = function() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); });
};

var WSSE = function(username, secret) {
    this.username = username;
    this.secret = secret;
};

WSSE.prototype.getHeader = function() {
    var nonce = uuid.v4();
    var created = new Date().toISOString();

    var hashString = nonce + created + this.secret;
    var digest = CryptoJS.SHA256(hashString).toString(CryptoJS.enc.Base64);
    var b64nonce = CryptoJS.enc.Latin1.parse(nonce).toString(CryptoJS.enc.Base64);

    var header = "UsernameToken";
    header += " Username=\"" + this.username + "\",";
    header += " PasswordDigest=\"" + digest + "\",";
    header += " Nonce=\"" + b64nonce + "\",";
    header += " Created=\"" + created + "\",";
    header += " Algorithm=\"SHA256\"";

    return { 'X-WSSE': header };
};

var wsse = new WSSE('************', '********************');
postman.setEnvironmentVariable("x-wsse", wsse.getHeader()['X-WSSE'])

0 个答案:

没有答案