开发者

python test script

开发者 https://www.devze.com 2023-01-23 11:42 出处:网络
I am trying to automate a test script for a website I have the following error import urllib , urllib2 , cookielib , random ,datetime,time,sys

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(), cod开发者_如何学Pythone, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden


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)


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.


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")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号