如何从html表单执行python脚本并返回结果

时间:2019-05-23 15:22:05

标签: javascript php python html

我有一个网站,该网站的表单允许用户上传文件(pdf或docx)。我正在尝试在加载的文件上执行一些python代码,然后返回并在HTML页面上打印该文件的文本。

经过研究,我了解到:

  • 由于html表单,我们称其为python脚本。该调用执行python脚本,该脚本通过post方法

  • 接收数据
  • python脚本执行其工作并返回带有结果的html代码。

对于html部分:

<html>
<head>
<title></title>
</head>
<body>

  <h1>Upload File</h1>
  <form action="example.py" method="POST" enctype="multipart/form-data">
      File: <input name="file" type="file">
      <input name="submit" type="submit">
  </form> 

</body>
</html>

对于python部分:

def convert_doc_to_text(filename):
    text = textract.process(filename) # extract text from file 
    return text.decode('utf-8')
  • 我可以使用哪种技术托管和执行python脚本?
  • 如何在网站服务器和python脚本服务器之间传输上载的文件?

谢谢。

1 个答案:

答案 0 :(得分:-1)

在PHP中使用shell_exec function

<?php     
$command = escapeshellcmd('path_to/test.py');
$output = shell_exec($command);
echo $output;
?>

从这里: Running a Python script from PHP