【问题标题】:Best Way to Structure Functions in Python在 Python 中构造函数的最佳方法
【发布时间】:2014-10-26 21:33:19
【问题描述】:

我目前正在寻找将我的常规公共 IP 地址与我也连接到互联网的 TOR IP 地址进行比较。我的代码如下:-

def public_ip():
    url = 'http://ifconfig.me/ip'
    request = urllib2.Request(url)
    request.add_header('Cache-Control','max-age=0')
    response = urllib2.urlopen(request)
    print response.read()

def connect_tor(): 
    LOCALHOST = "127.0.0.1"
    PORT = 9150

    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, LOCALHOST, PORT)
    socket.socket = socks.socksocket

    url = 'http://ifconfig.me/ip'
    request = urllib2.Request(url)
    request.add_header('Cache-Control','max-age=0')
    response = urllib2.urlopen(request)
    print response.read()

if __name__ == "__main__":
    public_ip()
    connect_tor()

然后我要做的是创建第三个函数,该函数将确认两个函数生成的 IP 地址的值,以确认它们不同。尽管如此,我怎样才能将前两个函数的 IP 地址提供给第三个函数。到目前为止,这是我的想法,但我不确定最佳选择:-

1.) 只需为所有三个函数创建一个大函数 2.)创建全局变量并将值返回给全局变量并在第三个函数中使用它们 3.) 从 'public_ip' 函数中调用 'connect_tor' 函数,将 'public_ip' 传递给 'connect_tor 函数',然后通过将两个 IP 作为参数传递来调用第三个函数。

这里的最佳做法或建议是什么?

【问题讨论】:

    标签: python python-2.7 tor


    【解决方案1】:

    我会为您当前的函数public_ip()connect_tor() 添加一个返回值。然后简单地比较它们:

    print public_ip() != connect_tor()
    

    这样您可以检查匿名性,同时完全避免使用其他功能。

    【讨论】:

      猜你喜欢
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多