我可以使用什么来将脚本中的文件参数从 html 代码传递给 cherrypy 函数
What can I use to pass the file argument in the script from the html code to cherrypy function
那么我该怎么做才能将参数传递给函数。
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.
的一个实例
那么我该怎么做才能将参数传递给函数。
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.