如何确保我在twython中落后于代理?

时间:2015-03-02 14:15:42

标签: python twitter twython

我使用TOR代理连接到Twython。但是我看到当我使用ip:port这样的666.666.666.666:666666时,它仍然会连接。那么,在使用twython时,我如何确保/断言我在代理后面连接?

from twython import Twython

client_args = {
    'proxies': {
        'socks5': '666.666.666.666:666666', # My TOR is 127.0.0.1:9000

    },
    'timeout': 300,
}

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, 
                  OAUTH_TOKEN_SECRET, client_args=client_args)

我也尝试用sudo tcpflow -i any -C -J port 9000监听端口9000,但是当我没有运行twython时它会获得流量......所以它是不确定的。

1 个答案:

答案 0 :(得分:0)

你可以创建一个IP检查方法,如下所示:

import urllib
import re

def IpCheck(request):
    theIP = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", request)
    ip = ''.join(theIP)
    return ip

url = 'http://checkip.dyndns.org'
realip = IpCheck(urllib.urlopen(url).read())
torip = IpCheck(#method that properly reads the website with tor like above) #I dont use twython 

设置代理,然后通过您正在使用的代理再次检查IP。

然后你可以检查Ip是否与

相同
if realip == torip:
    print('Tor Isn't Working!')
else:
    print('Tor Enabled')

或功能:

def TorCheck(realip, torip):
    if realip == torip:
        return('Whatever you want it to return')
    else:
        return('Tor Is Enabled!')

或使其成为一个功能:

def IpCheck(request, torrequest):
    theIP = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", request)
    torip = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}", torrequest)
    ip = ''.join(theIP)
    tip= ''.join(torip)
    if tip == ip:
        return('Whatever you want it to return')
    else:
        return('Tor Is Enabled!')# + 'Using Ip: ' + tip)
realip = urllib.urlopen(url).read()
torip = #Tor method that reads the website var url
#isTor = IpCheck(realip, torip)
#print IpCheck(realip, torip)
#or print isTor