简体   繁体   中英

Getting Tor Control data through Stem and presenting on the web with web.py

I have set up a Tor relay and would like it to display some statistics about itself on a webpage. Therefore I have installed lighttpd and web.py on the same box and it runs fine.

I have also installed Stem and I can get the data from the Tor control port succesfully using the Python example found here: https://stem.torproject.org/tutorials/the_little_relay_that_could.html

Now I would like to combine the two and have web.py run the script and output the data to the website. I have fiddled with it for hours and I am out of clues. How do I need to write the python web.py app? Here is an example of an attemp that does not work:

import web
from stem.control import Controller

urls = (
    '/', 'index'
)

class index:
    def GET(self):
        with Controller.from_port(port = 9051) as controller:
            controller.authenticate("mypassword")  # provide the password here if you set one

            bytes_read = controller.get_info("traffic/read")
            bytes_written = controller.get_info("traffic/written")

            return "My Tor relay has read %s bytes and written %s." % (bytes_read, bytes_written)

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

Thank you!

Your usage of stem looks right though you might want to look into BW events if you expand this to do multiple datapoints. As for web.py, the examples on their site return a view.render.base instance, maybe that's the missing bit?

Well, I got it working now. Embarrasing, but it seems like all I did wrong was forgetting quotation marks around the password. I also moved the app = web.application(urls, globals()) up before the class. Don't now if it makes a difference. Now, on to bandwidth events to make a live updating web page with stats.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM