2012-07-09 67 views
0

我刚刚得到的错误:的Python:实例没有属性 'sendAMail'

AttributeError: SecondLife instance has no attribute 'sendAMail' 

的哪些错误? (我检查了格式化,这不是错误 我检查了语法,也没有错误。)

脚本发生什么是一个网址打开与饼干,我想从它的一些信息。

import urllib2, cookielib, re 
import ClientForm 
import re 
import smtplib 

kurse = ['Entwicklung von Multimediasystemen', 'Computergrafik', 'Gestaltung von Multimediasystemen', 'Verteilte Systeme'] 

class SecondLife: 

    def __init__(self, usernames, password): 
     self.username = usernames 
     self.password = password 
     self.url = 'https://lsf.htw-berlin.de/qisserver/rds?state=user&type=0&application=QISPOS' 

     cookiejar = cookielib.LWPCookieJar() 
     cookiejar = urllib2.HTTPCookieProcessor(cookiejar) 
     # debugger = urllib2.HTTPHandler(debuglevel=1) 

     opener = urllib2.build_opener(cookiejar) 
     urllib2.install_opener(opener) 

    def sendAMail(self, smtp_server, user, password, listener, subject, text): 
     smtp = smtplib.SMTP(smtp_server) 
     smtp.starttls() 
     smtp.login(user,password) 
     msg = "SUBJECT: " + subject + "\n\n" + text 
     smtp.sendmail("[email protected]", listener, msg) 
     smtp.quit() 

    def login(self): 
     response = urllib2.urlopen(self.url) 
     forms = ClientForm.ParseResponse(response, backwards_compat=False) 

     # forms[0] is 'GET', forms[1] is 'POST' 
     form = forms[0] 

     try: 
      form['username'] = self.username 
      form['password'] = self.password 
     except Exception, e: 
      print 'The following error occured: \n"%s"' % e 
      print 
      print 'A good idea is to open a browser and see if you can log in from there.' 
      print 'URL:', self.url 

      exit() 

     self.page = urllib2.urlopen(form.click('submit')).read() 

    def friends_online(self): 

     self.login() 

     final = "" 
     final_asi = "" 
     leistungsstand = "" 
     match = re.search(r"asi=\w*\d*\"", self.page) 

     if match: 
      final = match.group() 
      final_asi = re.sub("asi=", "", final) 
      final_asi = re.sub("\"", "", final_asi) 

      print "vorher: " + final 
      print "nachher: " + final_asi 

      leistungsstand_url = "https://lsf.htw-berlin.de/qisserver/rds?state=htmlbesch&application=sospos&moduleParameter=Student&navigationPosition=functions%2Cnotenspiegel&breadcrumb=notenspiegel&topitem=functions&subitem=notenspiegel&asi=" + final_asi 
      leistungsstand = urllib2.urlopen(leistungsstand_url).read() 
     else: 
      print "not match" 



     # Ausloggen 
     logout = "https://lsf.htw-berlin.de/qisserver/rds?state=user&type=4&re=last&menuid=logout&category=auth.logout" 
     urllib2.urlopen(logout).read() 

     website = open("lsf.html", "w") 
     website.write(leistungsstand) 
     website.close() 

     for kurs in kurse: 
      print kurs 

      if (re.search(kurs, "fajfjsjj Entwicklung von Multimediasystemen hahahah")): 
       self.sendAMail("smtp.googlemail.com", "user", "passw", "[email protected]", "kurs" , "Eine neue Note ist im LSF eingetragen.") 


     #self.final_asi.replace(new, "asi=","") 
     #asi[0].replace("\"","") 


     #print "Final " + asi 





SL = SecondLife('xyz', 'xyz') 
SL.friends_online() 

回答

3

对我的作品:从实例中打印出self.sendAMail

<bound method SecondLife.sendAMail of <__main__.SecondLife instance at 0x101d91e18>> 

我认为这是一个格式问题,虽然。如果我复制并粘贴代码并查看空白,我会看到空格和制表符混合使用。特别是:

In [20]: [line for line in d if 'def' in line] 
Out[20]: 
['  def __init__(self, usernames, password):\n', 
' \tdef sendAMail(self, smtp_server, user, password, listener, subject, text):\n', 
'  def login(self):\n', 
'  def friends_online(self):\n'] 

def sendAMail\t看起来非常可疑。我75%确定不一致的空白是导致问题的原因。尝试使用python -tt scriptname.py运行脚本,这会导致标签使用不一致的错误。

+0

如果事实证明是一个混合使用空白的错误,这将是一个OP永远不会忘记的教训。这就是为什么源代码在使用空格时应该是一致的。 – inspectorG4dget 2012-07-09 00:31:10

+0

@DSM也为我工作在2.7。 – 2012-07-09 01:49:36

+0

感谢这是一个空白的错误! :) – Furtano 2012-07-09 12:31:14

相关问题