html调用python脚本_用html按钮运行python脚本

1586010002-jmsa.png

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

url = input('URL: ')

It uses modules to check headers for URL validity, takes the valid URL and downloads the first image from the webpage (URL) using Beautiful Soup. It works perfectly as a standalone Python script that I can run in my terminal/command prompt.

What I'd like to build is a webpage that utilizes my Python script and allows users to enter a URL form a web interface. Example:

Download

The URL entered from the input would then be passed into the python script when the user clicks on the Download button.

解决方案

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值