使用Python进行文件上传和处理

Rio*_*ams 6 python file-upload web

几天前我的一位朋友找到了我 - 他的编程经验很少,他有一个项目,他要求帮助.

基本上 - 这是他试图完成的事情:

1.) Create a website that can accept text files as input.
2.) Read said file and pass the parameters contained in the 
    file to a python script.
3.) Output these results of the script on the same webpage upon completion.
Run Code Online (Sandbox Code Playgroud)

他知道少量Python(足以编写处理脚本),但他不知道从哪里开始.我使用读取文件的ASP页面为他做了一个快速示例,并使用IronPython将参数传递到脚本文件并输出结果,这正如我预期的那样工作.

然而 - 对他来说,我希望能指导他朝着正确的方向发展一个更简单的应用程序来执行此操作,并希望找到一些建议或听取有关不同方法的一些想法.我认为由于他缺乏经验,越简单越好.

多谢你们.

Ble*_*der 6

Flask非常简单,功能强大且直观.我喜欢它了Django的对于较小的项目,如Django使用方式太多的文件夹(只要按照入门教程).这就是我简单直观的意思.我无法用文字解释它,所以这是一个示例脚本:

文件: script.py

app = Flask(__name__)
app.config.from_object(__name__)

@app.route('/')
def index():
  return render_template('index.html', message = 'Hello')

if __name__ == '__main__':
  app.run(host = '0.0.0.0')
Run Code Online (Sandbox Code Playgroud)

文件: index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  <head>
    <title>Test</title>
  </head>

  <body>
  {% if message != 'nope' %}
    {{ message }}
  {% endif %}
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

只是我的想法,祝你好运.