简体   繁体   中英

Python: instance has no attribute 'sendAMail'

I just get the error:

AttributeError: SecondLife instance has no attribute 'sendAMail'

Whats wrong? (I checked the formating and this is not the error. I checked the syntax and also not the error.)

What in the script happens is that an url gets open with cookies and i want some information from it.

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("bln.schade@gmail.com", 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", "bln.schade@gmail.com", "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()

Works for me: printing out self.sendAMail from within an instance gives

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

I think it is a formatting issue, though. If I copy and paste your code and look at the whitespace, I see mixed use of spaces and tabs. In particular:

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']

The \\t before def sendAMail looks very suspicious. I'm 75% sure the inconsistent whitespace is what's causing the problem. Try running your script using python -tt scriptname.py , which will throw an error about inconsistent tab usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM