Python のテキスト ファイルから URL リンクを変数として読み取り、それを html で使用する必要があります。テキスト ファイル "file.txt" には " http://188.xxx.xxx.xx:8878 " という 1 行しか含まれていません。この行は変数 "link" に保存する必要があります。ボタン画像「go_online.png」をクリックしたときにリンクが開かれるように、htmlを変更します。次のようにコードを変更しようとしましたが、うまくいきません! 助けてください?
#!/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)