Log-In Automation
Question: I am trying to write a Python script that will automate logging in to a web-
client. This is to automatically log-in to the web-client with a provided user
name and password. Below is my Python code:

    
    
    import httplib 
    import urllib 
    import urllib2 
    header = {
        'Host' : 'localhost.localdomain',
        'Connection' : 'keep-alive',
        'Origin' : 'localhost.localdomain', #check what origin does
        'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131029 Firefox/17.0',
        'Content-Type' : 'application/x-www-form-urlencoded',
        'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Referer' : 'http://localhost.localdomain/mail/index.php/mail/auth/processlogin',
        'Accept-Encoding' : 'gzip, deflate',
        'Accept-Language' : 'en-US,en;q=0.5',
        'Cookie' : 'atmail6=tdl3ckcf4oo88fsgvt5cetoc92'
    }
    content = {
        'emailName' : 'pen.test.clin',
        'emailDomain' : '',
        'emailDomainDefault' : '',
        'cssStyle' : 'original',
        'email' : 'pen.test.clin',
        'password' : 'aasdjk34',
        'requestedServer' : '',
        'MailType' : 'IMAP',
        'Language' : ''
    }
    
    def runBruteForceTesting():
        url="http://localhost.localdomain/mail/index.php/mail/auth/processlogin"
        for i in range (0,100):
            data = urllib.urlencode(content)
            request = urllib2.Request(url, data, header)
            response = urllib2.urlopen(url, request)
            print 'hi'
            print request, response
    runBruteForceTesting()
    

However: I am getting the following error:

    
    
    Traceback (most recent call last):
      File "C:/Users/dheerajg/Desktop/python/log.py", line 39, in <module>
        runBruteForceTesting()
      File "C:/Users/dheerajg/Desktop/python/log.py", line 35, in runBruteForceTesting
        response = urllib2.urlopen(url, request)
      File "C:\Python27\lib\urllib2.py", line 127, in urlopen
        return _opener.open(url, data, timeout)
      File "C:\Python27\lib\urllib2.py", line 402, in open
        req = meth(req)
      File "C:\Python27\lib\urllib2.py", line 1123, in do_request_
        'Content-length', '%d' % len(data))
      File "C:\Python27\lib\urllib2.py", line 229, in __getattr__
        raise AttributeError, attr
    AttributeError: __len__
    


Answer: The `request` object that you received from `urllib2.Request` does not have a
`__len__` method ; in your context, it means you're calling `urllib2.urlopen`
with a wrong second argument.

Looking at documentation, it is written it needs a string:

> data may be a string specifying additional data to send to the server, or
> None if no such data is needed.

So what about calling `urlopen` like this:

> response = urllib2.urlopen(url, request.get_data())

?

