python - 自动生成 tor 导出节点列表

标签 python tor

我想自动生成一个可以到达特定 IP 地址的 tor 导出节点列表。我在网上搜索了一会儿,从 http://sjoerd-hemminga.com/blog/2012/10/block-tor-exit-nodes-using-iptables/ 中找到了这段代码。

if [[ -z "$1" ]]; then
  echo Usage: $0 "<your host's ip>"
  exit 1
fi

hostip=$1

for i in $(wget https://check.torproject.org/cgi-bin/TorBulkExitList.py\?ip=$hostip -O- -q |\
    grep -E '^[[:digit:]]+(\.[[:digit:]]+){3}$'); do
  sudo iptables -A INPUT -s "$i" -j DROP
done

谁能帮助我更好地理解这段代码,因为每次我尝试运行它时,它都会产生错误。

欢迎任何替代答案,但我希望它们是用 Python 编写的。

最佳答案

import os
import re
import sys
import urllib

if len(sys.argv) != 2:
    print "Usage {} <your host's ip>".format(sys.argv[0])
    sys.exit(1)

hostip = sys.argv[1]

u = urllib.urlopen('https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=' + hostip)
for ip in u:
    ip = ip.strip()
    if re.match('\d+(\.\d+){3}$', ip):
        #print ip
        os.system('sudo iptables -A INPUT -s "{}" -j DROP'.format(ip))
u.close()

关于python - 自动生成 tor 导出节点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16977923/

相关文章:

python - C中如何判断一个内存页是否被mmaped

perl - 为什么 Perl 可能允许使用 TOR 而不是 https 的 http 网站?

python - 使用 Tor + Privoxy 抓取谷歌购物结果 : How to avoid block?

python - 如何使用 Python 连接到 Tor 浏览器

python - 通过Tor连接时为什么不提供cookie?

python - Tensorflow:tf.nn.conv2d 实际执行在哪里?

python - 如何拟合数据集以便它们共享一些(但不是全部)参数值

python - Pandas 数据帧类型错误: quantile() missing 1 required positional argument: 'quantile' ?

python - 无法将所有文件移动到新文件夹

Python - 通过代理使用 socket.gethostbyname