Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
157 lines (129 sloc) 2.89 KB

title:使用Tor配置代理 categories:Python


安装MAC port

国内ip

$ curl -O https://distfiles.macports.org/MacPorts/MacPorts-2.4.0.tar.bz2`
$ tar xf MacPorts-2.4.0.tar.bz2
$ cd MacPorts-2.4.0/
$ ./configure
$ make
$ sudo make install
$ /opt/local/bin/port version 
Version: 2.4.0 # 已成功
$ 

通过port安装tor

$ sudo /opt/local/bin/port install tor
$ cd /opt/local/etc/tor
$ mv torrc.sample torrc
$ /opt/local/bin/tor -f /opt/local/etc/torrc

很遗憾国内连接会失败。。。

Centos 安装 tor

美国服务器

$ yum install openssl
$ yum install libevent-devel
$ wget https://www.torproject.org/dist/tor-0.2.9.9.tar.gz
$ tar -zxvf tor-0.2.9.9.tar.gz
$ cd tor-0.2.9.9
$ ./configure
$ make
$ tor-0.2.9.9/src/or/tor

服务启动成功

代码查看IP更换情况

  1. 依赖库

    $ sudo yum install python-devel
    $ pip install requesocks
    $ pip install 'requests[socks]'
    $ pip install --upgrade setuptools
    $ pip install bs4
    $ pip install PySocks
    $ pip install requests[security]
    $ pip install pyOpenSSL
    
  2. 安装nc指令

    $ yum install nmap-ncat.x86_64
    
  3. 生成tor密码

    $ cd
    $ tor-0.2.9.9/src/or/tor --hash-password "这里更换成你的密码"
    这里会生成一串数格式(16:一串数)
    $ vim torrc
    找到 HashedControlPassword 16:原来的那串数
    修改为 HashedControlPassword 16:一串数
    保存退出
    $ tor-0.2.9.9/src/or/tor -f torrc
    
  4. 代码

    • 新开终端窗口执行下面代码
    # -*- coding: utf-8 -*-
    import time
    import requests
    from bs4 import BeautifulSoup
    
    import os
    
    
    def get_soup(url):
        try:
            proxies = {
                "http": "socks5://{ip}".format(ip='kongfm.com:9050'),
                "https": "socks5://{ip}".format(ip='kongfm.com:9050'),
            }
            response = requests.get(url, proxies=proxies)
            soup = BeautifulSoup(response.text, 'html.parser')
            print soup
            return soup
        except Exception as e:
            raise e
    
    url = 'https://api.ipify.org?format=json'
    
    
    def main():
        while 1:
            time.sleep(10)
            print "current tor IP..."
            get_soup(url)
            os.system("""(echo authenticate '"这里更换成你的tor密码"'; echo signal newnym; echo quit) | nc kongfm.com 9051""")
    
    
    if __name__ == "__main__":
        main()
    
    
  5. 执行结果

    current tor IP...
    {"ip":"199.249.223.75"}
    250 OK
    250 OK
    250 closing connection
    current tor IP...
    {"ip":"163.172.221.209"}
    250 OK
    250 OK
    250 closing connection
    current tor IP...
    {"ip":"176.126.252.11"}
    250 OK
    250 OK
    250 closing connection
    current tor IP...
    {"ip":"176.126.252.11"}
    250 OK
    250 OK
    250 closing connection
    current tor IP...
    {"ip":"65.19.167.132"}
    250 OK
    250 OK
    250 closing connection
    current tor IP...
    {"ip":"94.242.246.24"}
    250 OK
    250 OK
    250 closing connection
    
You can’t perform that action at this time.