简体   繁体   English

如何在Azure上运行Python脚本?

[英]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) 上运行良好。

Most likely you haven't created an Azure Run As Account .您很可能还没有创建Azure Run As Account Follow the steps here to do so.请按照此处的步骤进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 VM 上的文件使用 Azure 数据工厂运行 python 脚本? - How to run python script with Azure Data Factory using files on a VM? 在 Azure Webjobs 中运行 Python 脚本 - Run Python script in Azure Webjobs 在运行 python 脚本的我的 azure 服务器上,Ram 使用量不断增加 - Ram usage is constantly increasing on my azure server running python scripts 在 python Azure Function App 中运行可执行文件 - Run an executable in python Azure Function App 我们如何在 Azure 中运行标准的 Python 脚本并将文件保存到数据湖? - How can we run a standard Python script in Azure and save files to a Data Lake? 如何通过 Chef 运行 sh 脚本并显示 echo output - How to run sh scripts by Chef and show echo output 无法从 python 文件运行 azure 数据块 - Not able to run azure databricks from python file 无法在 Azure Functions v4 中运行 PowerShell 脚本,但可以在控制台应用程序中运行 - Cannot Run PowerShell Scripts in Azure Functions v4 but works in console app 有没有办法在Azure中部署一个Python的代码,并在webjob中运行? - Is there a way to deploy a Python code in Azure and run it in webjob? 如何将 python 脚本 arguments 传递给 Azure CLI `az ml run submit-script` 命令? - How do I pass in python script arguments to Azure CLI `az ml run submit-script` command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM