在Azure中,我可以从C#WebJob调用Python脚本吗?

时间:2017-07-10 09:42:29

标签: c# python azure azure-webjobs

我想在C#中创建一个WebJob。不幸的是,我需要使用Python 3脚本,因为目前我不需要使用C#执行特定任务的合适库。

例如,请参阅此answer/example

是否可以让我的WebJob调用Python脚本?我可以将Python3脚本放在blob容器中 - 然后我可以从C#WebJob调用并执行它吗?

2 个答案:

答案 0 :(得分:1)

Sure, it's possible to call a Python 3 script via C# WebJob that follow the sample you linked. First of all, you need to install a Python 3 runtime as below.

  1. Access the kudu tool via the url https://<your webapp name>.scm.azurewebsites.net, and follow the figure below to install a Python 3 runtime. enter image description here
  2. For example to install Python 3.5.2 x86, it will be installed in the path D:\home\Python35, then you just need to change the Python execute file path in the sample to try to run it.

Hope it helps.


Update: Install pip tool & other Python packages.

  1. Access the url https://<your azure webapp name>.scm.azurewebsites.net/DebugConsole.
  2. Commands as below.

    D:\home>cd Python35
    D:\home\Python35>curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    
    100 1558k  100 1558k    0     0  6829k      0 --:--:-- --:--:-- --:--:-- 7179k
    D:\home\Python35>python get-pip.py
    Requirement already up-to-date: pip in d:\home\python35\lib\site-packages
    Collecting wheel
      Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    Installing collected packages: wheel
    Successfully installed wheel-0.29.0
    
  3. For example, install numpy package

    D:\home\Python35>python get-pip.py numpy
    Collecting numpy
      Downloading numpy-1.13.1-cp35-none-win32.whl (6.8MB)
    Installing collected packages: numpy
    Successfully installed numpy-1.13.1
    

答案 1 :(得分:0)

不知道这是否是最佳方法,但这是我过去的做法:

  • 创建一个python webjob(手动,触发)(see tutorialCreate Azure Webjob - Manual Triggered

  • 创建一个C#webjob。

  • 从C#作业中触发Python作业:

    using (var client = new HttpClient())
    {
        var username = "jobusername";
        var password = "jobpassword";
        var byteArray = Encoding.ASCII.GetBytes($"{username}:{password}");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
        var response = await client.PostAsync("joburl", null);
    }
    

您可以在azure门户网站上的作业属性中找到作业凭据: Azure webjob properties