我需要从
python中的文本文件中读取一个url链接作为变量,并在html中使用它.
文本文件“file.txt”只包含一行“
http://188.xxx.xxx.xx:8878”,这行应该保存在变量“link”中,然后我应该在html中使用这个变量的包含,这样当我打开链接时点击按钮图片“go_online.png”.我试着改变我的代码如下,但它不起作用!有什么帮助吗?
#!/usr/bin/python
import cherrypy
import os.path
from auth import AuthController, require, member_of, name_is
class Server(object):
_cp_config = {
'tools.sessions.on': True,
'tools.auth.on': True
}
auth = AuthController()
@cherrypy.expose
@require()
def index(self):
f = open ("file.txt","r")
link = f.read()
print link
f.close()
html = """
var var_link = '{{ link }}';
{htmlText}
"""
myText = ''
myText = "Hellow World"
return html.format(htmlText=myText)
index.exposed = True
#configuration
conf = {
'global' : {
'server.socket_host': '0.0.0.0', #0.0.0.0 or specific IP
'server.socket_port': 8085 #server port
},
'/images': { #images served as static files
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.abspath('/home/ubuntu/webserver/images')
}
}
cherrypy.quickstart(Server(), config=conf)