標籤:script 計算 auth ofo arp span hmac ams 需要
參考資料:
https://codefying.com/2015/10/02/executing-a-python-script-from-a-c-program/
demo:
https://github.com/zLulus/NotePractice/tree/dev3/Console/CSharpUsingPythonDemo
代碼
string progToRun = "test.py"; char[] spliter = { ‘\r‘ }; Process proc = new Process(); proc.StartInfo.FileName = "python.exe"; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.UseShellExecute = false; string psw = "123456"; string parameters2 = "haha"; //檔案路徑+參數集合 proc.StartInfo.Arguments = string.Concat(progToRun, " ", psw.ToString()," ", parameters2.ToString()); proc.Start(); StreamReader sReader = proc.StandardOutput; string[] output = sReader.ReadToEnd().Split(spliter); foreach (string s in output) Console.WriteLine(s); proc.WaitForExit(); //取出計算結果 Console.WriteLine(output[0]); Console.Read();
import base64import hmacimport hashlibimport urllibimport urlparseimport jsonimport urllibimport sysdef _auth_data(): psw=sys.argv[1] parameters2=sys.argv[2] md5 = hashlib.md5() md5.update(psw.encode(‘utf-8‘)) md5.update(‘hello‘.encode(‘utf-8‘)) s = json.dumps({"assetPwd": md5.hexdigest()}) r=urllib.quote(s, safe=‘‘) print r return rif __name__ == ‘__main__‘: _auth_data()
需要注意的是:
(1)樣本的python版本是2.7,可以用python3,這個和你的環境有關
學習過程中遇到的比較揪心的問題也是“python2找不到python3的庫、方法”之類的問題,保證環境一致並且正確啊╮(╯_╰)╭
(2)python基本文法問題,縮排要麼是空格,要麼是tab,保持一致,否則VS會報錯
C#調用python