C#調用python

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.