Thread.run() does not get called.

Rob Hall bloke at ii.net
Wed Oct 2 02:47:56 EDT 2002


'''OS = Win 98
Python Version = 2.2
SSL package installed
'''

I have the following program which logs onto a website in its own thread and
stores the cookie locally.  Please not I have changed my username an
password.


'''
LogOn.py
Log on to the broker website and keep the cookie alive by
saving it to a text file.  It runs in a separate thread
so the calling module is not held up.
The saved cookie can then be read by any other process
that needs access to the website in the same session.
'''
import urllib
import urllib2
import ClientCookie
import os
import threading


class LogOn(threading.Thread):
    '''Log on to the broker website and save the cookie.

    Attributes:
    userName -- The username to use when logging on
    password -- The password to use when logging on
    logonAddress -- The web address that handles logging on
    cookieLocation -- The local address for storing the cookie
    '''

    def __init__(self, userName, password, \
                 logonAddress =
'https://www.quicktrade.com.au/cgi-bin/login', \
                 cookieLocation = None):
        # First set the default directory for cookie storage.
        # Use the os module to extend the pathname
        # so we can keep the module platform independent.
        cookieFileName = 'cookie.txt'
        if cookieLocation:
            self.cookieFile = os.path.join(cookieLocation, cookieFileName)
        else:
            self.cookieFile = os.path.join(os.getcwd(), cookieFileName)
        self.userName = userName
        self.password = password
        self.status = 0

        threading.Thread.__init__( self, name='logon')


    def run(self):
        # Overloads the run method in Thread.
        # Log onto the website.
        userPassword = urllib.urlencode({'USERNAME': self.userName, \
                                         'PASSWORD': self.password})
        try:
            self.request = urllib2.Request('%s?%s' % (logonAddress,
userPassword))
            self.result = urllib2.urlopen(self.request)
            self.__saveCookie()
        except:
            self.status = 0 # Fail status.


    def __saveCookie(self):
        '''Save the cookie to the specified location.'''
        c = ClientCookie.Cookies(ignore_discard=1)
        c.extract_cookies(self.result, self.request)
        if c.as_string() is '':
            # If there are no cookies we can assume we were not able
            # to log onto the site.  Therefore return a fail status.
            print c.as_string()
            self.status = 0 # Failed - could not get the cookie
        else:
            c.save(self.cookieFile)
            self.status = 1 # Succeed status


if __name__ == '__main__':
    '''This is the test routine.'''
    import time

    logonAddress = 'https://www.quicktrade.com.au/cgi-bin/login'
    userName = 'myUserName'
    password = 'myPassword'

    testLogon = LogOn( userName, password, logonAddress )
    testLogon.start()
    while testLogon.isAlive():
        print 'Waiting...'
        time.sleep(1)
    testLogon.join()

    if testLogon.status:
        print 'Currently logged on and cookie saved in CWD as ../cookie.txt'
        print 'The page reads as follows...\n\n'
        htmlText = testLogon.result.read()
        print htmlText
    else:
        print 'Log on failed.'


It all works fine when run from the command line.

However, when I try to call it fro another module, it doesn't run the run()
method.  eg:

''' Some_Other_Module.py'''
if __name__ == '__main__':
    '''This is the test routine.'''
    from LogOn import LogOn

    logonAddress = 'https://www.quicktrade.com.au/cgi-bin/login'
    userName = 'myUserName'
    password = 'myPassword'

    print 'Logging on...'
    testLogon = LogOn( userName, password, logonAddress )
    testLogon.start()
    print 'Waiting for logon to finish...'
    testLogon.join()

It just falls straight through!  In fact, if I run it through the debugger,
it always shows up as dead.  When start() is called, for some reason it
never goes to the overloaded run() method.

Any help greatly appreciated.

Rob






More information about the Python-list mailing list