改变R中的Tor身份

时间:2012-06-18 19:49:23

标签: r ip tor rcurl

我将Tor与R结合使用,并希望为每个新请求更改我的IP。我的代码如下:

library(RCurl)
opts <- list(proxy="127.0.0.1", proxyport=8118)
for (i in 1:10)
  {
  con <- socketConnection(host="127.0.0.1",port=9051)  # DOES NOT WORK
  writeLines("signal newnym", con=con)                 # DOES NOT WORK
  ip <- getURL("http://ifconfig.me/ip", .opts = opts)  
  print(ip)
  Sys.sleep(1)
  }  

我可以通过Tor连接,但是标记为“DOES NOT WORK”的两条线似乎没有得到正确的信号到Tor,所以IP保持不变。

问候!

1 个答案:

答案 0 :(得分:8)

我遇到了类似的问题,但是在安装了Privoxy作为http代理并按照here的说明进行设置之后设法使其工作。然后,这是我在R中使用的代码:

library(RCurl)
# check current IP address
print(getURL("http://ifconfig.me/ip"))
# proxy options
opts <- list(proxy="127.0.0.1", proxyport=8118)
# opening connection with TOR
con <- socketConnection(host="127.0.0.1",port=9051)
print(getURL("http://ifconfig.me/ip", .opts = opts))  

for (i in 1:10)
    {
    writeLines('AUTHENTICATE \"password\"\r\nSIGNAL NEWNYM\r\n', con=con)
    Sys.sleep(5)
    print(getURL("http://ifconfig.me/ip", .opts = opts)) 
    Sys.sleep(5)
    }  

确保使用TCP连接的手动设置,地址为127.0.0.1:9051,验证方法为“password”,将上面代码中双引号之间的密码替换为您设置的密码。

相关问题