Resources:
https://codefying.com/2015/10/02/executing-a-python-script-from-a-c-program/
Demo
Https://github.com/zLulus/NotePractice/tree/dev3/Console/CSharpUsingPythonDemo
Code
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 json Import Urllibimport sysdef Span class= "Hljs-title" >_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 ()
It is important to note that:
(1) The Python version of the example is 2.7 and can be used Python3, which is related to your environment
Learning process encountered in the more worried problem is "Python2 can not find Python3 library, method" and so on, to ensure that the environment is consistent and correct AH ╮ (╯_╰) ╭
(2) Python basic syntax problem, indentation is either a space, or tab, consistent, otherwise vs will error
C # calling Python