如何确保我在 twython 的代理后面?

How to ensure I'm behind the proxy in twython?

我正在使用 TOR 代理连接到 Twython。但是我看到,当我使用 666.666.666.666:666666 之类的假 ip:port 时,它仍然可以连接。那么,我如何才能 ensure/assert 在使用 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 时它也会有流量...所以它没有定论。

您可以创建一个 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