python test script
Question: I am trying to automate a test script for a website

I have the following error

    
    
        import urllib , urllib2 , cookielib , random ,datetime,time,sys
    
    
      cookiejar = cookielib.CookieJar() 
      urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
      urllib2.install_opener(urlOpener)
    
    
      username=username.strip()
    
      values = {'username': username, 'password': 'password'}  #user input
    
      data = urllib.urlencode(values)
    
     request = urllib2.Request('http://141.168.25.182/',data)
    
     url = urlOpener.open(request)
    
    
        File "<stdin>", line 1, in ?
     File "/usr/lib/python2.4/urllib2.py", line 364, in open
    response = meth(req, response)
    File "/usr/lib/python2.4/urllib2.py", line 471, in http_response
    response = self.parent.error(
    File "/usr/lib/python2.4/urllib2.py", line 402, in error
    return self._call_chain(*args)
    File "/usr/lib/python2.4/urllib2.py", line 337, in _call_chain
    result = func(*args)
    File "/usr/lib/python2.4/urllib2.py", line 480, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 403: Forbidden
    


Answer: A few suggestions that might help you

**urlencode function is different from what you think**

    
    
    >>> from urllib import urlencode
    >>> values = {'username': 'username', 'password': 'password'}
    >>> data = urlencode(values)
    >>> data
    'username=username&password=password'
    >>> 
    

**request method arguments are again incorrect**

When you do this the, data is the request payload

    
    
    request = urllib2.Request('http://141.168.25.182/',data)
    

**What you want to do is authenticate yourself.**

This will depend on what type of authentication server expects. The following
serves for basic authentication using urllib2. Read the module docs for more
detail.

    
    
    import urllib2
    auth_handler = urllib2.HTTPBasicAuthHandler()
    auth_handler.add_password(user='..', passwd='...')
    opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)
    urllib2.urlopen('url)
    

