2016-09-29 44 views
1

那麼我可以做些什麼來將參數傳遞給函數。我可以使用什麼來將腳本中的文件參數從html代碼傳遞到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) 
+0

你試圖通過什麼樣的論據? – thesonyman101

回答

1

看看到文件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屬性獲得更多的信息,這是一個蟒蛇TemporaryFile的一個實例。

相關問題