python控制tor的源代码

第一个源代码

====================================

import socks

import socket

from stem import Signal

from stem.control import Controller

with Controller.from_port(port = 9151) as controller:

controller.authenticate(password)

controller.signal(Signal.NEWNYM)

socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9150)

socket.socket = socks.socksocket

def getaddrinfo(*args):

return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo

r = requests.get('http://icanhazip.com/')

print r.content

# Returns a different IP

==================================================

哈希密码:

tor --hash-password my_password

修改配置文件:

sudo gedit /etc/tor/torrc

添加这些行

ControlPort 9051

HashedControlPassword 16:E600ADC1B52C80BB6022A0E999A7734571A451EB6AE50FED489B72E3DF

CookieAuthentication 1

注: - HashedControlPassword从第一命令生成

第二个源代码

#----------------Cut Here-------------------------------------------------------------------------

import stem

from stem import Signal

from stem.control import Controller

from stem.connection import connect

import time

#

# Create a new controller

#

controller = Controller.from_port()

Password = "My_Personal_Password"

#

def renew_tor():

global controller

global Password

print ('Renewing Tor Circuit')

if "stem.control.Controller" not in str(controller):

#if global controller exist no more

controller = Controller.from_port()

# debug output

print (controller)

# authenticare the connection with the server control port

controller.authenticate(Password)

print ('Tor running version is : %s' % controller.get_version())

# force a new circuit

controller.signal(Signal.NEWNYM)

# wait for new circuit

time.sleep(10)

print ('New Tor circuit estabilished')

if __name__ == "__main__":

for i in range (10000):

print (" Attempt n. : %i " % i)

renew_tor()

#----------------Cut Here(end)--------------------------------------------

第三个源代码===========================================================

import time

import socket

import socks

import requests

from bs4 import BeautifulSoup

from stem import Signal

from stem.control import Controller

controller = Controller.from_port(port=9051)

def connectTor():

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5 , "127.0.0.1", 9050, True)

socket.socket = socks.socksocket

def getaddrinfo(*args):

return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo

def renew_tor():

controller.authenticate()

controller.signal(Signal.NEWNYM)

def showmyip():

r = requests.Session()

page = r.get(url)

soup = BeautifulSoup(page.content, "lxml")

ip_address = soup.find("span",{"class":"ip_address"}).text.strip()

print(ip_address)

for i in range(10):

renew_tor()

connectTor()

showmyip()

time.sleep(10)

=========================================================

具体启动tor、建立tcp转发、同时监听端口流量,可以找我咨询

-------------------------------------------------------------------------------------------------------

请支持一下,谢谢!

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181209G09R4W00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

扫码关注云+社区

领取腾讯云代金券