我需要从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 = """
<html>
<script language="javascript" type="text/javascript">
var var_link = '{{ link }}';
</script>
<body>
<p>{htmlText}
<p>
<a href={{ var_link }} ><img src="images/go_online.png"></a>
</body>
</html>
"""
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)