First post here as I've just started using Tor. I have a server running on a machine with a hidden service on port 50001. I'm trying to connect to it from my laptop using the server hidden service onion address and send it json objects to retrieve data. I am able to do this fine over my local network using sockets (not using tor or onion address) but haven't been able to get it to work over tor.

I'm able to create a tor instance but I'm trying to use sockets with the proxy but how do I specify the onion address I want to communicate with? The onion address doesn't contain an IP that sockets needs. So not sure how to do this. Showing what I have so far below.

socks_proxy = ('', 9050)

onion_service_address = 'my server onion address'
onion_service_port = 50001

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    # Connect to the Tor SOCKS proxy

    # Send the Tor-specific handshake to establish a connection

    # Receive the response to the handshake
    response = s.recv(2)

    # Check if the response indicates a successful connection
    if response == b'\x05\x00':
        # JSON request to send to the onion service
        json_data= {"id": 1, "jsonrpc": "2.0", "method": method, "params": []}

        # Construct the onion service address with its port
        onion_service_address_with_port = f"{onion_service_address}:{onion_service_port}"

        # Connect to the onion service through the Tor SOCKS proxy

        # Receive the response to the connection request
        response = s.recv(10)

        # Check if the response indicates a successful connection
        if response == b'\x05\x00\x00\x03':
            # Serialize the JSON data to bytes
            json_bytes = json.dumps(json_data).encode("utf-8")

            # Send the JSON data

            # Receive and print the response
            response = s.recv(4096)

            print("Failed to connect to the onion service.")

        print("Failed to connect to the Tor SOCKS proxy.")


You must log in to answer this question.

Browse other questions tagged .