Python使用Tor发送请求报错:WinError10061连接被主动拒绝求助
Hey there! Let's break down this connection error you're hitting with Requests and Tor—WinError 10061 means the target (Tor's SOCKS proxy, in this case) is actively refusing your connection, so let's walk through the most common fixes step by step:
1. Ensure Tor is up and running
First things first: if Tor isn't running, your code can't connect to its proxy. On Windows, either open the Tor Browser and wait for it to fully connect (look for the "Connected to the Tor network" status in the toolbar) or confirm the standalone Tor service is running (check Windows Services for "Tor Win32 Service"). Don't run your code until Tor's fully ready!
2. Double-check your proxy configuration in code
Requests relies on correct proxy settings to route traffic through Tor. By default, Tor uses 127.0.0.1:9050 for standalone services, and 127.0.0.1:9150 for Tor Browser (since it uses an isolated instance). A common mistake is using the wrong port, or forgetting the socks5h:// prefix (the h tells Tor to handle DNS resolution, which avoids leaks and fixes some connection issues).
Here's a working example to reference:
import requests # Use 9150 if you're using Tor Browser instead of standalone Tor proxies = { 'http': 'socks5h://127.0.0.1:9050', 'https': 'socks5h://127.0.0.1:9050' } try: response = requests.get('http://httpbin.org/ip', proxies=proxies) print(response.text) except Exception as e: print(f"Error: {e}")
3. Check for firewall/antivirus blocks
Windows Firewall or your antivirus software might be blocking Python from connecting to Tor's port. Try temporarily disabling them (just for testing!) and running your code again. If it works, add Python and Tor to your firewall's allowed apps list to fix this permanently.
4. Verify Tor's listening port
Sometimes Tor's configuration gets modified, so it's worth checking the torrc file to confirm the SOCKS port matches what you're using in code:
- For Tor Browser: The file lives in
Tor Browser\Browser\TorBrowser\Data\Tor\torrc - For standalone Tor service: Typically in
C:\Tor\torrc(default installation path)
Look for the line starting withSocksPort—it should be set to either9050or9150(whichever you're using in your code).
5. Try switching to Tor Browser's proxy
If you've been using the standalone Tor service and hitting issues, give Tor Browser a shot. Open it, wait for it to connect, then update your code to use port 9150 instead of 9050. Tor Browser's instance is often more reliable for beginners, as it handles configuration automatically.
If none of these steps fix the problem, share your exact code snippet and whether you're using Tor Browser or the standalone Tor service—I can help dig deeper!
内容的提问来源于stack exchange,提问作者Sid

