python - 我可以使用什么将脚本中的文件参数从html代码传递到cherrypy函数

标签 python cherrypy

那么我该怎么做才能将参数传递给函数。

import os, random, string
import cherrypy


class StringGenerator(object):

    @cherrypy.expose        
    def index(self):        
      return """<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- The above 3 meta tags *must* come first in the head; any other head 

content must come *after* these tags -->

    <title>Bootstrap 101 Template</title>




    <!-- Bootstrap -->

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 
rel="stylesheet">



  </head>

  <body>

   <div class="jumbotron">


    <p ><h4 class='text-success'  style='margin-left:30%;'>This is the twitter 
word select </h1></p>


     <form class="form-inline" action="generate/" style='margin-left:20%;' 
enctype= "multipart/form-data">




  <div class="form-group" >


    <label for="exampleInputName2">Enter the file name</label>


    <input type="file" class="form-control" name="file1" id="file" placeholder=" enter the file ">


  </div><div class="form-group" >


    <label for="exampleInputName2">Enter the prefrence</label>


    <input type="text" class="form-control" name="length" id="exampleInputName2" placeholder=" enter the preference ">


  </div>


  <button type="submit" class="btn btn-primary">Result</button>


</form>



  </div> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->


    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>


    <!-- Include all compiled plugins (below), or include individual files as needed -->


    <script src="js/bootstrap.min.js"></script>

  </body>


</html>"""


    @cherrypy.expose            
    def generate(self, length,file1):            
        some_string = "harsh"+length        
        cherrypy.session['mystring'] = some_string        
        fp = open(file1)      
        return fb

    @cherrypy.expose
    def display(self):    
        return cherrypy.session['mystring']

if __name__ == '__main__':
    conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },


        '/static': {

            'tools.staticdir.on': True,
            'tools.staticdir.dir': './public'
        }
    }
    cherrypy.quickstart(StringGenerator(), '/', conf)

最佳答案

查看文件 cherrypy files tutorial ,但为了给您提供具体的答案,您的 generate 方法必须使用 file1 参数的 file 属性。

例如,此方法可以工作:

@cherrypy.expose            
def generate(self, length, file1):            
    some_string = "harsh" + length        
    cherrypy.session['mystring'] = some_string        
    file_content = file1.file.read()
    return file_content

您还可以操作file属性来获取更多信息,它是一个python实例 TemporaryFile .

关于python - 我可以使用什么将脚本中的文件参数从html代码传递到cherrypy函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39762027/

相关文章:

python - 如何按python pandas中的值范围列表进行分组

python - 使用 CherryPy 的 REST WebService 的友好 URL

python - CherryPy 与 Django

将 CSS 合并为 HTML 的 Python 代码

python - 在 matplotlib 中叠加 pcolormeshes

python - 使用 Selenium 单击 'Load More' 按钮直到它不存在(Youtube)

python - 跟踪日志文件

python - 提取检测到的近似形状和边界框Mask RCNN

python - 使用 cherrypy 流式传输文件

python - cherrypy 是如何工作的?当并发率低时,与 Tornado 相比,它可以很好地处理请求