【问题标题】:How to make a django rest api run a python file in my computer如何让 django rest api 在我的电脑上运行 python 文件
【发布时间】:2020-02-29 05:47:41
【问题描述】:

我正在为 Android 应用程序制作 API。只要将输入提供给 API,我就需要 API 为我执行计算机中的 python 文件。我正在考虑将我的计算机用作试用服务器。我希望你能理解我的问题。请告诉我如何从 API 运行 python 文件

【问题讨论】:

    标签: python django rest api django-rest-framework


    【解决方案1】:

    您可以看到当有人访问该 url 时,它将启动另一个进程:

    from django.http import HttpResponse
    from django.views.generic import View
    import subprocess
    
    class ExecutePythonFileView(View):
        def get(self, request):
            # Execute script
            subprocess.call(['python', 'somescript.py'])
    
            # Return response
            return HttpResponse("Executed!")
    

    【讨论】:

    • @ShraveenB.S 如果您觉得答案有帮助,请将其标记为正确答案,谢谢!
    猜你喜欢
    • 2012-01-24
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多