4

I need to read a url link from text file in python as a variable, and use it in html. The text file "file.txt" contains only one line "http://188.xxx.xxx.xx:8878", this line should be saved in the variable "link", then I should use the contain of this variable in the html, so that the link should be opened when I click on the button image "go_online.png". I tried to change my code as following but it doesn't work! any help please?

#!/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)
3
  • what page would that URL resolve to? Commented Feb 28, 2013 at 14:09
  • the url is my server, its my external address with an open port. Commented Feb 28, 2013 at 14:16
  • The URL can be also any web page such as www.google.com ,......etc. Commented Feb 28, 2013 at 14:23

1 Answer 1

4

first off, not sure that the javascript part makes any sense, just leave it out. Also, your opening a p tag but not closing it. Not sure what your templating engine is, but you could just pass in the variables in pure python. Also, make sure to put quotes around your link. So your code should be something like:

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()
        f.close()
        myText = "Hello World" 
        html = """
        <html>
            <body>
                <p>%s</p>          
                <a href="%s" ><img src="images/go_online.png"></a>
            </body>
        </html>
        """ %(myText, link)        
        return html
    index.exposed = True

(btw, the %s things are string placeholders, that will be poplulated the variables in %(firstString, secondString) at the end of the the multi line string.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for reply, but sorry it doesn't work.I tried with return html.format(htmlText=myText, link) but not work! can you please rewrite the complete code?
Thanks Hoff, its work now.In my original cod, the variable "myText =' ' " will has a new strings values according to some "if"s conditions so that I must use "return html.format(htmlText=myText)".So do have another solution so that let me use return html.format(htmlText=myText)?
I have to say too, THANK YOU :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.