如何在 Azure 上 运行 Python 脚本?

How to run Python scripts on Azure?

我正在尝试 运行 Azure 中的一些 Python 代码,这是有史以来第一次。我用 Google 搜索 'how to run Python scripts in Azure' 并发现了以下内容 link。

https://docs.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual-python-3

本质上,我需要这样做。

通过选择 MyFirstRunbook-Python3 窗格中的“编辑”打开文本编辑器。

添加以下代码以向 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)

当我 运行 该代码时,出现此错误。

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

我需要 'cert'、'pks12_cert' 还是 'pem_pkey'?我不认为我有那些。我什至不确定那些东西是什么。同样,我正在尝试将我的 Azure 环境设置为 运行 一个 Python 脚本,它在我的桌面 (Spyder) 上运行良好。

您很可能还没有创建 Azure Run As Account。按照步骤 here 进行操作。