AttributeError:addinfourl实例没有__call__方法

时间:2013-02-23 14:50:57

标签: python

第一次传递一切正常,但第二次运行我得到了AttributeError:实例没有__ addinfourl call__方法,我不知道是什么问题

第一遍 真正 状态:好的

第二次传递文件“D:\ python \ New Status \ status.py”,第124行,在print mainstatus.bite()文件“D:\ python \ New Status \ status.py”,第109行,咬一口s = file(“cc.beat”)AttributeError:addinfourl实例没有调用方法

基本上用它来检查服务器的状态并确认另一个进程是否处于活动状态

    failure = True
    servestatus = False


    def log():
        a=open ("Status.failures","a")
        now = datetime.now()
        tiempo = str (now) +"  -  Error\n"
        a.write(tiempo)
        a.close ()

    class mainstatus(object):

        @staticmethod
        def xmlstatus():
            try:        
                file = urllib2.urlopen('http://192.168.1.110:9900/admin/xmlstatus?user=&password=')
                data = file.read()
                file.close()
                dom = parseString(data)
                xmlTag = dom.getElementsByTagName('Status')[0].toxml()
                xmlData=xmlTag.replace('<Status>','').replace('</Status>','')
                if xmlTag == "<Status>OK</Status>":
                    stat = True
            except:
                stat= False
            return stat

        @staticmethod
        def hola():
            x = "test"
            return x

        @staticmethod
        def internet(url):
            try:
                response=urllib2.urlopen(url,timeout=1)
                return True
            except urllib2.URLError as err: pass
            return False

        @staticmethod
        def heartbeat():
            a=open ("heart.beat","w")
            now = datetime.now()
            tiempo = str (now.minute)
            a.write(tiempo)
            a.close ()

        @staticmethod
        def push(mensaje):
            #Miguel
            conn = httplib.HTTPSConnection("api.pushover.net:443")
            conn.request("POST", "/1/messages.json",
              urllib.urlencode({
                "token": "",
                "user": "",
                "message": mensaje,
              }), { "Content-type": "application/x-www-form-urlencoded" })
            conn.getresponse()
            #Gerswin
            conn = httplib.HTTPSConnection("api.pushover.net:443")
            conn.request("POST", "/1/messages.json",
              urllib.urlencode({
                "token": "",
                "user": "",
                "message": mensaje,
              }), { "Content-type": "application/x-www-form-urlencoded" })
            conn.getresponse()

        @staticmethod
        def colam():
            cola = 0
            for root, dirs, files in os.walk("\\\\Server0\\smsdata\\Q"):
                    for file in files:    
                            if file.endswith('.req'):
                                    cola += 1
        @staticmethod
        def wol():
            HOST = "192.168.1.1"
            user = "root"
            password = "16745665"
            tn = telnetlib.Telnet(HOST)
            tn.read_until("login: ")
            tn.write(user + "\n")
            if password:
                tn.read_until("Password: ")
                tn.write(password + "\n")
            tn.write("/usr/sbin/wol -i 192.168.1.255 -p 7 00:11:25:36:08:FE\n")
            tn.write("exit\n")
            tn.read_all()

        @staticmethod
        def bite(): 
            now = datetime.now()
            tiempo = (now.minute)
            s = file("cc.beat")
            status = int(s.readline())
            s.close()
            vivo = tiempo - status    
            if (vivo > 0):
                return False
            else:
                return True

    count = 5
    print "Server Status Check: Runing..."

    while (count < 9):

        time.sleep(2)
        print mainstatus.bite()
        pulso = True
        if pulso == False:
            #os.startfile('check.exe')
            print "activa"
        else:
            pass
        status = mainstatus.internet("http://192.168.1.110:9900")
        mainstatus.heartbeat()
        if status == True:
          if   mainstatus.xmlstatus() == True:
              print '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bStatus:OK',

              if failure == True:
                  accion = os.system('net stop "NowSMS"')
                  file = urllib2.urlopen('http://sql.gerswin.com/status.php?status=OK')
                  data = file.read()
                  file.close()
                  failure = False

              if mainstatus.colam() >= 20 and servestatus == False:
                  accion = os.system('net start "NowSMS"')
                  mainstatus.push("Server Overload, Server 2 Running")
                  servestatus = True
              else:
                  if mainstatus.colam() < 20 and servestatus == True:
                      mainstatus.push("Stoping Server 2")
                      accion = os.system('net stop "NowSMS"')
                      servestatus = False
                      file = urllib2.urlopen('http://sql.gerswin.com/status.php?status=OK')
                      data = file.read()
                      file.close()

          else:
              print '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bStatus: Modem Failure',
              mainstatus.wol()        
              if servestatus == False:
                  accion = os.system('net start "NowSMS"')
                  mainstatus.push("Modem Failure, Server 2 Running")
                  log()
                  file = urllib2.urlopen('http://sql.gerswin.com/status.php?status=2')
                  data = file.read()
                  file.close()
                  servestatus = True

        else:
            print "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bStatus: Fallo Server",
            mainstatus.wol()
            if servestatus == False:           
                accion = os.system('net start "NowSMS"')
                file = urllib2.urlopen('http://192.168.1.110:9900?ok=no')
                data = file.read()
                file.close()
                log()
                mainstatus.push("Server Failure, Server 2 Running")
                servestatus = True

1 个答案:

答案 0 :(得分:2)

您正在使用全局变量file来存储urllib2.urlopen()次调用的结果。但您也希望将其用作内置file() type

>>> import urllib2
>>> file('/dev/random')
<open file '/dev/random', mode 'r' at 0x10c8ee660>
>>> file = urllib2.urlopen('http://stackoverflow.com')
>>> file('/dev/random')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: addinfourl instance has no __call__ method

不要那样做。不要使用您自己的变量名影视内置插件,不要使用file()。请改用open() function

>>> open('/dev/random')
<open file '/dev/random', mode 'r' at 0x10c8ee6f0>
>>> response = urllib2.urlopen('http://stackoverflow.com')