0

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
asked Nov 3, 2010 at 9:14
2
  • what is this error printed on the screen Commented Nov 3, 2010 at 9:18
  • Hint 1. Post the actual question in the title. "python test script" is not a question. It's a very, very bad title. Hint 2: Format your code correctly so the indentation is correct. Hint 3: Actually have a question. An error message is not a question. Unless you take the hint and fix your question, you may not get much help at all. Commented Nov 3, 2010 at 10:03

3 Answers 3

1

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)
answered Nov 3, 2010 at 9:26
Sign up to request clarification or add additional context in comments.

Comments

0

I suggest using mechanize. That way it would less hassles. Your code would look something like this

import mechanize
base_url = "http://someurl"
login_url = base_url + "/login"
protected_url = base_url + "/potected"
b = mechanize.Browser()
b.visit(login_url)
b.add_password(base_url, "user", "secret", "some Realm")
b.open(login_url) 
b.open(protected_url)

You can also use mechanize's form submission and .click methods.

answered Nov 3, 2010 at 10:40

Comments

0

If you want to automate tests for a web application you should use selenium and do something like.

from selenium import webdriver
# Create an instance of a browser
browser = webdriver.Firefox()
# Open homepage
browser.get("http://141.168.25.182")
# Find username and password fields (i do not know what ids your input fields actually have)
username_input = browser.find_element_by_id("username")
password_input = browser.find_element_by_id("password")
# Fill in username and password fields
username_input.send_keys("test_username")
password_input.send_keys("test_password")
# Find and click on the login button
browser.find_element_by_id("login").click()
# Check that you are signed in by finding a sign out button
browser.find_element_by_id("signout")
answered Jul 15, 2016 at 12:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.