简体   繁体   中英

Integrating python code with Visual Studio c#

I have created GuI in Visual Studio 2019.

在此处输入图像描述

There user will enter username and password and that i have to pass to python script. That when user will click on login button, python script will be triggered and output will be displayed.

My Tried python code is:

import paramiko
import time

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:

    hostname = input("Enter host IP address: ")
    username = input("Enter SSH Username: ")
    password = input("Enter SSH Password: ")
    port = 22


    ssh.connect(hostname, port, username, password, look_for_keys=False)
    print("ssh login successfully")



#stdin,stdout,stderr = ssh.exec_command('show version')
#output = stdout.readlines()
#print(output)


    Device_access = ssh.invoke_shell()
    Device_access.send(b'environment no more \n')
    Device_access.send(b'show version\n')
    time.sleep(2)
    output = Device_access.recv(65000)
    print (output.decode('ascii'))

except:
    print("error in connection due to wrong input entered")

But in this i am not getting how to link with input enter to Gui c# with python script. Please let me know how i can do.

Thanks in advance!

You could use arguments to call your Python Script.

Change the python script:

import paramiko
import time
import sys # Used to get arguments

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:

   hostname = sys.argv[1] # Skip 0th arg, since it is just the filename
   username = sys.argv[2]
   password = sys.argv[3]
   port = 22

   ssh.connect(hostname, port, username, password, look_for_keys=False)
   print("ssh login successfully")

   #stdin,stdout,stderr = ssh.exec_command('show version')
   #output = stdout.readlines()
   #print(output)


   Device_access = ssh.invoke_shell()
   Device_access.send(b'environment no more \n')
   Device_access.send(b'show version\n')
   time.sleep(2)
   output = Device_access.recv(65000)
   print (output.decode('ascii'))

except:
       print("error in connection due to wrong input entered")

And change your C# code which calls the Script to something like this:

Process pythonScript = new Process();
pythonScript.StartInfo.FileName = "Your python script";
pythonScript.StartInfo.Arguments = $"{YouHostnameVar} {YouUsernameVar} {YourPasswordVar}"; // Start the script with the credentials as arguments
pythonScript.Start();

There are multiple approaches to incorporating a Python script with .NET C# code

I will try and give a basic overview, along with my suggestion, but ultimately, it will be up to you to figure out what works best.

IronPython

IronPython is an actual separate interpreter to translate Python code into the .NET Common Language Runtime (CLR). It works well for simple Python2 scripts that are not reliant on certain libraries.

Python.NET

Python.NET uses the normal Python interpreter. It simply provides a way to interface between Python scripts and .NET code.

System Diagnostics (My Suggestion)

The System Diagnostics C# tool allows you to run Python scripts as a system process. Not that this only runs the Python script. In order to transfer information between the Python script and the C# code, you will need some kind of shared file. I recommend setting up a folder where you save information used by both the C# and Python programs.

For a simple implementation of System Diagnostics, along with notes on the particular way System Diagnostics is being called, check out this: https://www.dotnetlovers.com/article/216/executing-python-script-from-c-sharp

EDIT Based on Paul Sütterlin Answer

As opposed to using a file to share information, Paul correctly points out that you can pass information as arguments. He also points out the simple process tool in C#, which is easier to set up than System Diagnostics. I recommend you read the article I linked to see which solution best suits you. System diagnostics gives you more options, but they do have to be configured.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM