lum*_*ked 5 python redhat web2py bottle flask
我想在服务器设置中使用它们之前尝试使用Redhat kickstart文件并在python中修改它们.我的应用程序使用python来卷曲我的Redhat Satellite服务器上的原始kickstart文件然后我正在对kickstart文件中的某些值进行字符串替换.当我在python中卷曲文件时,它会以多行字符串的形式返回,这是我需要的,以便redhat kickstart正确解释文件.但是当我通过其中一个框架(web2py,bottle,flask)返回字符串变量时,会发生一些事情并且它不会将其作为多行字符串返回,我需要它来保留除了区域之外的原始文件的确切格式我改变.我不想把我的kickstart文件放在模板中,因为我通过卫星管理它们,如果我从卫星卷曲文件然后它拿起任何修改,而不需要进入模板的时间.然后在模板或其他东西中我返回没有模板的字符串或模板文件中我只将1个变量作为整个kickstart文件传递给模板.
@route('/kickstart/<name>')
def kickstart(name):
ks = vula.kickstarter.kickstart.Kickstarter()
ks_file = ks.getKickstartFile()
return pystache.render('{{kickstart}}', {'kickstart': ks_file})
Run Code Online (Sandbox Code Playgroud)
这是我的vula包中的方法.它完全按照我需要的方式返回文件.但是在这之间又发生了一些事情,并通过框架返回这个值.
def getKickstartFile(self):
response = urllib2.urlopen('https://my-satellite-server/core-kickstarter')
ks_file = response.read()
return ks_file
Run Code Online (Sandbox Code Playgroud)
我开始使用Bottle作为框架,但我发现一条声明说他们无法返回多行字符串,所以请抓一下.我搬到了Flask,但目前Flask正在做同样的事情.我还在学习python,可能我做错了什么,但我需要任何帮助才能使这个工作正常.我想输出一个多行字符串.我知道你也使用了
""" or '''
Run Code Online (Sandbox Code Playgroud)
对于多行字符串,但即使你这样做并通过框架发送它仍然会作为一个连续的行打印到屏幕上.我究竟做错了什么?作为最后的手段,如果我不能输出多行字符串,我将被迫将kickstart文件放入模板中.
Sea*_*ira 10
Bottle和Flask都能很好地处理多线串.您的问题是您的数据被text/html默认解释为HTML,并且在显示时,任何空白组合都会折叠到一个空格中.为了确保您的数据完全按照您发送的方式返回,您需要将Content-Type标头设置为text/plain.
在烧瓶中:
# If you want *all* your responses to be text/plain
# then this is what you want
@app.after_request
def treat_as_plain_text(response):
response.headers["content-type"] = "text/plain"
return response
# If you want only *this* route to respond
# with Content-Type=text/plain
@app.route("/plain-text")
def a_plain_text_route():
response = make_response(getKickstartFile())
response.headers["content-type"] = "text/plain"
return response
Run Code Online (Sandbox Code Playgroud)
在瓶子里:
@route("/plain-text")
def plain_text():
response.content_type = "text/plain"
return """This
multi-line string
will show up
just fine"""
Run Code Online (Sandbox Code Playgroud)
肖恩的答案是正确的方法,但如果你只是想测试一些东西,你可以使用标签xmp:
@app.route("/"):
def hello():
return """<xmp>
Hello,
Multiline!
</xmp>"""
Run Code Online (Sandbox Code Playgroud)
您还可以为此创建自己的装饰器:
from functools import wraps
def multiline(fn):
@wraps(fn)
def _fn(*args, **kwargs):
return "<xmp>" + fn(*args, **kwargs) + "</xmp>"
return _fn
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做:
@app.route("/"):
@multiline
def hello():
return """Hello,
Multiline!"""
Run Code Online (Sandbox Code Playgroud)