Certainly, here's the amended post with the snippet from the torrc file included:
Title: Python script using Tor to fetch IP through Chrome encounters stem.`
SocketError: [Errno 61] Connection refused
,
Problem:
I'm attempting to develop a Python script that utilizes Tor to open Chrome and retrieve my IP address. However, I keep encountering the following error: stem.SocketError: [Errno 61] Connection refused. Despite enabling and running Tor on my Mac and ensuring that the torrc file settings are correct, the issue persists.
Here's a snippet of my code:
from fake_useragent import UserAgent
from stem import Signal
from stem.control import Controller
import time
from selenium import webdriver
import stem
def new_tor_id():
with Controller.from_port(port=9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
# Set up Selenium WebDriver
options = webdriver.ChromeOptions()
options.add_argument('--proxy-server=socks5://127.0.0.1:9050') # Configure Tor proxy
options.add_argument(f'user-agent={UserAgent().random}') # Random user agent
driver = webdriver.Chrome(options=options)
try:
new_tor_id()
print("New Tor identity requested")
time.sleep(1) # Allow time for the browser to open
except stem.SocketError as e:
print("Error:", e)
new_tor_id()
time.sleep(5) # Wait for Tor to establish a new identity
try:
time.sleep(1) # Allow time for the browser to open
# Open the website
driver.get("https://myip.is/")
time.sleep(10) # Allow time for the page to load
except Exception as e:
print("Error:", e)
finally:
# Close the browser window
driver.quit()
Snippet from torrc file:
ControlPort 9051
HashedControlPassword 16: 'Password'
CookieAuthentication 1
Request:
Could someone assist in identifying the error in my code? I've verified that Tor is enabled on my Mac and the torrc file contains the correct settings. However, I'm still encountering the stem.SocketError: [Errno 61] Connection refused error. Any insights or suggestions would be greatly appreciated. Thank you!
I attempted to utilize except stem.SocketError as e:, but unfortunately, I'm unable to pinpoint the underlying issue.