run python script with html button

后端 未结 1 667
予麋鹿
予麋鹿 2020-12-05 22:34

I have a Python script a.py that prompts for a URL:

url = input(\'URL: \')

It uses modules to check headers for URL validity,

相关标签:
1条回答
  • 2020-12-05 22:49

    There are a couple of ways to achieve this. You could use Apache modules like mod_python or mod_wsgi (which adheres to wsgi python standard), you could use a cgi script or you can use PHP (which is fairly common in web environments) to execute the python script through exec or system function.

    I think the easiest one would be just use PHP.

    exec('python /path/to/file.py');
    

    Edit: I just realized that my solution only describes how to run the python script.

    You could pass the url input through an argument on python.

    $url = $_GET['url'];
    exec("python /path/to/file.py $url");
    

    This is potentially dangerous even with user input validation.

    To execute it through a button, you could do an AJAX call. (jQuery example)

    $('#button').on('click', function() {
      $.get('python.php', {url : 'url' });
    });
    
    0 讨论(0)
提交回复
热议问题