我需要從Python中的文本文件中讀取一個url鏈接作爲變量,並在html中使用它。 文本文件「file.txt」,但只包含一個行「http://188.xxx.xxx.xx:8878」,此行應該被保存在變量「鏈接」,那麼我應該使用包含在HTML這個變量的,所以該鏈接,應打開時我點擊按鈕圖像「go_online.png」。我試圖改變我的代碼如下,但它不工作!請幫忙嗎?如何將python變量傳遞給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)
什麼網頁將是URL決心? – Makoto 2013-02-28 14:09:47
的URL是我的服務器,它的我與開放端口的外部地址。 – Linux 2013-02-28 14:16:59
URL可以是也可以是任何網頁,如www.google.com,......等等。 – Linux 2013-02-28 14:23:58