gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:06:11 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com