0

I want to create an Onion-Website with CherryPy in Python (using Stem to access the Tor network). My problem is the deployment of the site. For that I adopted the example from this article: https://jordan-wright.com/blog/2014/10/06/creating-tor-hidden-services-with-python/ (great article, but originally in combination with Flask, but this should also work with CherryPy and vice versa). Only difference is that Flask operates on port 5000 and CherryPy on 8080 (I changed that in the code below).

My final test-code looks like this:

import cherrypy
from stem.control import Controller


class HelloWorld(object):
    @cherrypy.expose
    def index(self):
        return "Hello world!"


if __name__ == '__main__':
    
    port = 8080
    host = "127.0.0.1"
    hidden_svc_dir = '/home/python/'

    print(" * Getting controller")
    controller = Controller.from_port(address="127.0.0.1", port=9151)
    try:
        controller.authenticate(password="")
        controller.set_options([
            ("HiddenServiceDir", hidden_svc_dir),
            ("HiddenServicePort", "80 %s:%s" % (host, str(port)))
            ])
        svc_name = open(hidden_svc_dir + "/hostname", "r").read().strip()
        print(" * Created host: %s" % svc_name)
    except Exception as e:
        print(e)
        
    cherrypy.quickstart(HelloWorld())
    

My /etc/tor/torrc contains this:

ControlPort 9051
HashedControlPassword xxx
CookieAuthentication 1

(I also tried with/without Password and with/without Cookies set to 1 and 0 - all with the same error message.)

Though in other codes I use Stem / Tor and they work properly, this one throws the folling error message:

 * Getting controller
Traceback (most recent call last):
  File "/home/xxx/.local/lib/python3.6/site-packages/stem/socket.py", line 535, in _make_socket
    control_socket.connect((self.address, self.port))
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "onion_server_1.py", line 20, in <module>
    controller = Controller.from_port(address="127.0.0.1", port=9151)
  File "/home/xxx/.local/lib/python3.6/site-packages/stem/control.py", line 1033, in from_port
    control_port = stem.socket.ControlPort(address, port)
  File "/home/xxx/.local/lib/python3.6/site-packages/stem/socket.py", line 503, in __init__
    self.connect()
  File "/home/xxx/.local/lib/python3.6/site-packages/stem/socket.py", line 172, in connect
    self._socket = self._make_socket()
  File "/home/xxx/.local/lib/python3.6/site-packages/stem/socket.py", line 538, in _make_socket
    raise stem.SocketError(exc)
stem.SocketError: [Errno 111] Connection refused

I have no more ideas why this does not work.

A working code (adopted from mine above) would be great.

Ulrich
  • 249
  • 4
  • 10

0 Answers0