簡體   English   中英

將 python 代碼與 Visual Studio c# 集成

[英]Integrating python code with Visual Studio c#

我在 Visual Studio 2019 中創建了 GUI。

在此處輸入圖像描述

用戶將輸入用戶名和密碼,我必須將其傳遞給 python 腳本。 當用戶單擊登錄按鈕時,將觸發 python 腳本並顯示 output。

我試過的 python 代碼是:

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")

但在這我不知道如何使用 python 腳本將輸入鏈接到 Gui c#。 請讓我知道我該怎么做。

提前致謝!

您可以使用 arguments 來調用您的 Python 腳本。

更改 python 腳本:

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")

並將調用腳本的 C# 代碼更改為如下所示:

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();

有多種方法可以將 Python 腳本與 .NET C# 代碼合並

我將嘗試給出一個基本的概述,以及我的建議,但最終,由你決定什么是最有效的。

鐵蟒

IronPython 是一個真正的獨立解釋器,用於將 Python 代碼翻譯成 .NET 公共語言運行時 (CLR)。 它適用於不依賴某些庫的簡單 Python2 腳本。

Python.NET

Python.NET 使用普通的 Python 解釋器。 它只是提供了一種在 Python 腳本和 .NET 代碼之間進行接口的方法。

系統診斷(我的建議)

系統診斷 C# 工具允許您將 Python 腳本作為系統進程運行。 並不是說這只運行 Python 腳本。 為了在 Python 腳本和 C# 代碼之間傳輸信息,您需要某種共享文件。 我建議設置一個文件夾來保存 C# 和 Python 程序使用的信息。

有關系統診斷的簡單實現,以及有關調用系統診斷的特定方式的說明,請查看以下內容: https://www.dotnetloves.com/article/216/executing-python-script-from-c-sharp

編輯基於 Paul Sütterlin 的回答

與使用文件共享信息相反,Paul 正確地指出您可以將信息作為 arguments 傳遞。 他還指出了 C# 中的簡單處理工具,它比 System Diagnostics 更容易設置。 我建議您閱讀我鏈接的文章,看看哪種解決方案最適合您。 系統診斷為您提供了更多選項,但必須對其進行配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM