C # Call python,

Source: Internet
Author: User

C # Call python,

References:
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"; // file path + parameter set 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 (); // retrieves the calculation result 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()

Note that:
(1) The python version of the example is 2.7. You can use python3, which is related to your environment.
The tough problem encountered during the learning process is also the problem of "python2 cannot find the library and method of python3", which ensures that the environment is consistent and correct.
(2) for basic python Syntax problems, indentation can be either a space or a tab to maintain consistency. Otherwise, VS will report an error.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.