How can I call a python script from an asp.net Azure app service?












0















I have an existing .NET Core / asp.net app service hosted on Azure. I need to call (on demand) a python script to return data based on custom user input.



It does not appear that I can use IronPython, since I need python modules that are built in CPython, which unfortunately aren't supported by IronPython.



The two options I see are:




  • I might be able to install the right python version and libraries on the app service and call it from the .NET code. This seems like it might be deprecated: https://docs.microsoft.com/en-us/visualstudio/python/publishing-python-web-applications-to-azure-from-visual-studio?view=vs-2017


  • I can create a whole new and separate app service for just the python script and call it as a REST API on demand from the .NET app service. This seems like overkill, and introduces the problem of opening up a whole new service publicly, which I don't want to do. This also appears to have the limitation that Flask isn't mean for production, so hosting many calls at once is not really workable. https://docs.microsoft.com/en-us/visualstudio/python/publish-to-app-service-windows?view=vs-2017



What is the best way to call a python script on demand from .NET app service on Azure?










share|improve this question

























  • If python script is simple, you can consider azure automation python runbook.

    – Ivan Yang
    Jan 2 at 6:15











  • I'll look into that. While the script is simple, the libraries it needs to use are definitely NOT simple. If it can't import advanced libraries, it won't work. That's the same reason I can't use IronPython.

    – John
    Jan 2 at 6:52











  • Ok, if any problem during the runbook, you can let me know. good luck:)

    – Ivan Yang
    Jan 2 at 6:59











  • It looks like that is really only for Azure automation, which isn't the problem I am trying to solve, and wouldn't work as an on demand service (SaaS) anyway.

    – John
    Jan 2 at 12:56
















0















I have an existing .NET Core / asp.net app service hosted on Azure. I need to call (on demand) a python script to return data based on custom user input.



It does not appear that I can use IronPython, since I need python modules that are built in CPython, which unfortunately aren't supported by IronPython.



The two options I see are:




  • I might be able to install the right python version and libraries on the app service and call it from the .NET code. This seems like it might be deprecated: https://docs.microsoft.com/en-us/visualstudio/python/publishing-python-web-applications-to-azure-from-visual-studio?view=vs-2017


  • I can create a whole new and separate app service for just the python script and call it as a REST API on demand from the .NET app service. This seems like overkill, and introduces the problem of opening up a whole new service publicly, which I don't want to do. This also appears to have the limitation that Flask isn't mean for production, so hosting many calls at once is not really workable. https://docs.microsoft.com/en-us/visualstudio/python/publish-to-app-service-windows?view=vs-2017



What is the best way to call a python script on demand from .NET app service on Azure?










share|improve this question

























  • If python script is simple, you can consider azure automation python runbook.

    – Ivan Yang
    Jan 2 at 6:15











  • I'll look into that. While the script is simple, the libraries it needs to use are definitely NOT simple. If it can't import advanced libraries, it won't work. That's the same reason I can't use IronPython.

    – John
    Jan 2 at 6:52











  • Ok, if any problem during the runbook, you can let me know. good luck:)

    – Ivan Yang
    Jan 2 at 6:59











  • It looks like that is really only for Azure automation, which isn't the problem I am trying to solve, and wouldn't work as an on demand service (SaaS) anyway.

    – John
    Jan 2 at 12:56














0












0








0








I have an existing .NET Core / asp.net app service hosted on Azure. I need to call (on demand) a python script to return data based on custom user input.



It does not appear that I can use IronPython, since I need python modules that are built in CPython, which unfortunately aren't supported by IronPython.



The two options I see are:




  • I might be able to install the right python version and libraries on the app service and call it from the .NET code. This seems like it might be deprecated: https://docs.microsoft.com/en-us/visualstudio/python/publishing-python-web-applications-to-azure-from-visual-studio?view=vs-2017


  • I can create a whole new and separate app service for just the python script and call it as a REST API on demand from the .NET app service. This seems like overkill, and introduces the problem of opening up a whole new service publicly, which I don't want to do. This also appears to have the limitation that Flask isn't mean for production, so hosting many calls at once is not really workable. https://docs.microsoft.com/en-us/visualstudio/python/publish-to-app-service-windows?view=vs-2017



What is the best way to call a python script on demand from .NET app service on Azure?










share|improve this question
















I have an existing .NET Core / asp.net app service hosted on Azure. I need to call (on demand) a python script to return data based on custom user input.



It does not appear that I can use IronPython, since I need python modules that are built in CPython, which unfortunately aren't supported by IronPython.



The two options I see are:




  • I might be able to install the right python version and libraries on the app service and call it from the .NET code. This seems like it might be deprecated: https://docs.microsoft.com/en-us/visualstudio/python/publishing-python-web-applications-to-azure-from-visual-studio?view=vs-2017


  • I can create a whole new and separate app service for just the python script and call it as a REST API on demand from the .NET app service. This seems like overkill, and introduces the problem of opening up a whole new service publicly, which I don't want to do. This also appears to have the limitation that Flask isn't mean for production, so hosting many calls at once is not really workable. https://docs.microsoft.com/en-us/visualstudio/python/publish-to-app-service-windows?view=vs-2017



What is the best way to call a python script on demand from .NET app service on Azure?







python asp.net azure






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 22:43







John

















asked Jan 1 at 10:11









JohnJohn

11




11













  • If python script is simple, you can consider azure automation python runbook.

    – Ivan Yang
    Jan 2 at 6:15











  • I'll look into that. While the script is simple, the libraries it needs to use are definitely NOT simple. If it can't import advanced libraries, it won't work. That's the same reason I can't use IronPython.

    – John
    Jan 2 at 6:52











  • Ok, if any problem during the runbook, you can let me know. good luck:)

    – Ivan Yang
    Jan 2 at 6:59











  • It looks like that is really only for Azure automation, which isn't the problem I am trying to solve, and wouldn't work as an on demand service (SaaS) anyway.

    – John
    Jan 2 at 12:56



















  • If python script is simple, you can consider azure automation python runbook.

    – Ivan Yang
    Jan 2 at 6:15











  • I'll look into that. While the script is simple, the libraries it needs to use are definitely NOT simple. If it can't import advanced libraries, it won't work. That's the same reason I can't use IronPython.

    – John
    Jan 2 at 6:52











  • Ok, if any problem during the runbook, you can let me know. good luck:)

    – Ivan Yang
    Jan 2 at 6:59











  • It looks like that is really only for Azure automation, which isn't the problem I am trying to solve, and wouldn't work as an on demand service (SaaS) anyway.

    – John
    Jan 2 at 12:56

















If python script is simple, you can consider azure automation python runbook.

– Ivan Yang
Jan 2 at 6:15





If python script is simple, you can consider azure automation python runbook.

– Ivan Yang
Jan 2 at 6:15













I'll look into that. While the script is simple, the libraries it needs to use are definitely NOT simple. If it can't import advanced libraries, it won't work. That's the same reason I can't use IronPython.

– John
Jan 2 at 6:52





I'll look into that. While the script is simple, the libraries it needs to use are definitely NOT simple. If it can't import advanced libraries, it won't work. That's the same reason I can't use IronPython.

– John
Jan 2 at 6:52













Ok, if any problem during the runbook, you can let me know. good luck:)

– Ivan Yang
Jan 2 at 6:59





Ok, if any problem during the runbook, you can let me know. good luck:)

– Ivan Yang
Jan 2 at 6:59













It looks like that is really only for Azure automation, which isn't the problem I am trying to solve, and wouldn't work as an on demand service (SaaS) anyway.

– John
Jan 2 at 12:56





It looks like that is really only for Azure automation, which isn't the problem I am trying to solve, and wouldn't work as an on demand service (SaaS) anyway.

– John
Jan 2 at 12:56












1 Answer
1






active

oldest

votes


















0














Per my experience, there are two ways to call a Python script in C# without IronPython.




  1. Directly use System.Diagnostics.Process in C# to run a command as same as the SO thread Run Command Prompt Commands to get the result via parse the content of the process standard output. Simply to do it, you can use py2exe to wrap a Python script as a .exe file to avoid for installing Python modules and setting environment variables on Azure App Service. However, considering for concurrency, it's not a good idea for performance.


  2. The second option as you said is to deploy a Python script as a REST API in the same instance of Azure Web App. You can follow the blog Deploying multiple virtual directories to a single Azure Website to deploy a flask app with your Python script as a child project via Visual Studio with PTVS to expose an API url like https://<your web app name>.azurewebsites.net/pyapi which can be called from your ASP.NET via HttpClient. I tried this solution, it works.



Note: Due to the restriction of Azure Web App sandbox for Local Address Requests, you have to use <your web app name>.azurewebsites.net as hostname, neither localhost or 127.0.0.1.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994612%2fhow-can-i-call-a-python-script-from-an-asp-net-azure-app-service%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Per my experience, there are two ways to call a Python script in C# without IronPython.




    1. Directly use System.Diagnostics.Process in C# to run a command as same as the SO thread Run Command Prompt Commands to get the result via parse the content of the process standard output. Simply to do it, you can use py2exe to wrap a Python script as a .exe file to avoid for installing Python modules and setting environment variables on Azure App Service. However, considering for concurrency, it's not a good idea for performance.


    2. The second option as you said is to deploy a Python script as a REST API in the same instance of Azure Web App. You can follow the blog Deploying multiple virtual directories to a single Azure Website to deploy a flask app with your Python script as a child project via Visual Studio with PTVS to expose an API url like https://<your web app name>.azurewebsites.net/pyapi which can be called from your ASP.NET via HttpClient. I tried this solution, it works.



    Note: Due to the restriction of Azure Web App sandbox for Local Address Requests, you have to use <your web app name>.azurewebsites.net as hostname, neither localhost or 127.0.0.1.






    share|improve this answer




























      0














      Per my experience, there are two ways to call a Python script in C# without IronPython.




      1. Directly use System.Diagnostics.Process in C# to run a command as same as the SO thread Run Command Prompt Commands to get the result via parse the content of the process standard output. Simply to do it, you can use py2exe to wrap a Python script as a .exe file to avoid for installing Python modules and setting environment variables on Azure App Service. However, considering for concurrency, it's not a good idea for performance.


      2. The second option as you said is to deploy a Python script as a REST API in the same instance of Azure Web App. You can follow the blog Deploying multiple virtual directories to a single Azure Website to deploy a flask app with your Python script as a child project via Visual Studio with PTVS to expose an API url like https://<your web app name>.azurewebsites.net/pyapi which can be called from your ASP.NET via HttpClient. I tried this solution, it works.



      Note: Due to the restriction of Azure Web App sandbox for Local Address Requests, you have to use <your web app name>.azurewebsites.net as hostname, neither localhost or 127.0.0.1.






      share|improve this answer


























        0












        0








        0







        Per my experience, there are two ways to call a Python script in C# without IronPython.




        1. Directly use System.Diagnostics.Process in C# to run a command as same as the SO thread Run Command Prompt Commands to get the result via parse the content of the process standard output. Simply to do it, you can use py2exe to wrap a Python script as a .exe file to avoid for installing Python modules and setting environment variables on Azure App Service. However, considering for concurrency, it's not a good idea for performance.


        2. The second option as you said is to deploy a Python script as a REST API in the same instance of Azure Web App. You can follow the blog Deploying multiple virtual directories to a single Azure Website to deploy a flask app with your Python script as a child project via Visual Studio with PTVS to expose an API url like https://<your web app name>.azurewebsites.net/pyapi which can be called from your ASP.NET via HttpClient. I tried this solution, it works.



        Note: Due to the restriction of Azure Web App sandbox for Local Address Requests, you have to use <your web app name>.azurewebsites.net as hostname, neither localhost or 127.0.0.1.






        share|improve this answer













        Per my experience, there are two ways to call a Python script in C# without IronPython.




        1. Directly use System.Diagnostics.Process in C# to run a command as same as the SO thread Run Command Prompt Commands to get the result via parse the content of the process standard output. Simply to do it, you can use py2exe to wrap a Python script as a .exe file to avoid for installing Python modules and setting environment variables on Azure App Service. However, considering for concurrency, it's not a good idea for performance.


        2. The second option as you said is to deploy a Python script as a REST API in the same instance of Azure Web App. You can follow the blog Deploying multiple virtual directories to a single Azure Website to deploy a flask app with your Python script as a child project via Visual Studio with PTVS to expose an API url like https://<your web app name>.azurewebsites.net/pyapi which can be called from your ASP.NET via HttpClient. I tried this solution, it works.



        Note: Due to the restriction of Azure Web App sandbox for Local Address Requests, you have to use <your web app name>.azurewebsites.net as hostname, neither localhost or 127.0.0.1.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 3:07









        Peter PanPeter Pan

        11.8k3824




        11.8k3824
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994612%2fhow-can-i-call-a-python-script-from-an-asp-net-azure-app-service%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            ts Property 'filter' does not exist on type '{}'

            mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window