[英]How to run Python scripts on Azure?
I am trying to run some Python code in Azure, for the first time ever.我正在尝试在 Azure 中运行一些 Python 代码,这是有史以来第一次。 I Googled 'how to run Python scripts in Azure' and came across the following link.我在谷歌上搜索“如何在 Azure 中运行 Python 脚本”并找到了以下链接。
https://learn.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual-python-3 https://learn.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual-python-3
Essentially, I need to do this.本质上,我需要这样做。
Open the textual editor by selecting Edit on the MyFirstRunbook-Python3 pane.通过在 MyFirstRunbook-Python3 窗格中选择编辑来打开文本编辑器。
Add the following code to authenticate to Azure:添加以下代码以对 Azure 进行身份验证:
import os
from azure.mgmt.compute import ComputeManagementClient
import azure.mgmt.resource
import automationassets
def get_automation_runas_credential(runas_connection):
from OpenSSL import crypto
import binascii
from msrestazure import azure_active_directory
import adal
# Get the Azure Automation RunAs service principal certificate
cert = automationassets.get_automation_certificate("AzureRunAsCertificate")
pks12_cert = crypto.load_pkcs12(cert)
pem_pkey = crypto.dump_privatekey(crypto.FILETYPE_PEM,pks12_cert.get_privatekey())
# Get run as connection information for the Azure Automation service principal
application_id = runas_connection["ApplicationId"]
thumbprint = runas_connection["CertificateThumbprint"]
tenant_id = runas_connection["TenantId"]
# Authenticate with service principal certificate
resource ="https://management.core.windows.net/"
authority_url = ("https://login.microsoftonline.com/"+tenant_id)
context = adal.AuthenticationContext(authority_url)
return azure_active_directory.AdalAuthentication(
lambda: context.acquire_token_with_client_certificate(
resource,
application_id,
pem_pkey,
thumbprint)
)
# Authenticate to Azure using the Azure Automation RunAs service principal
runas_connection = automationassets.get_automation_connection("AzureRunAsConnection")
azure_credential = get_automation_runas_credential(runas_connection)
When I run that code, I get this error.当我运行该代码时,出现此错误。
Failed
Traceback (most recent call last): File "C:\WPy64-3800\python-3.8.0.amd64\lib\automationassets.py", line 155, in _issue_request response = urllib.request.urlopen(request) File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 531, in open response = meth(req, response) File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 640, in http_response response = self.parent.error( File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 569, in error return self._call_chain(*args) File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 502, in _call_chain result = func(*args) File "C:\WPy64-3800\python-3.8.0.amd64\lib\urllib\request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp)urllib.error.HTTPError: HTTP Error 404: Not FoundDuring handling of the above exception, another exception occurred:Traceback (most recent call last): File "C:\Temp\nmcijfgi.bro\a761c289-67d9-493a-99b2-3ba7d46a7cd9", line 36, in <module> runas_connection = automationassets.get_automation_connection("AzureRunAsConnection") File "C:\WPy64-3800\python-3.8.0.amd64\lib\automationassets.py", line 232, in get_automation_connection connection = _client.get_connection_asset(name) File "C:\WPy64-3800\python-3.8.0.amd64\lib\automationassets.py", line 187, in get_connection_asset return self._issue_request(url, method=self._GET) File "C:\WPy64-3800\python-3.8.0.amd64\lib\automationassets.py", line 158, in _issue_request raise AutomationAssetNotFound()automationassets.AutomationAssetNotFound
Do I need 'cert', 'pks12_cert', or 'pem_pkey'?我需要“证书”、“pks12_cert”或“pem_pkey”吗? I don't think I have those.我不认为我有那些。 I'm not even sure what those things are.我什至不确定那些东西是什么。 Again, I am trying to setup my Azure environment to run a Python script, which works totally fine on my desktop (Spyder).同样,我正在尝试设置我的 Azure 环境以运行 Python 脚本,该脚本在我的桌面 (Spyder) 上运行良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.