Ace*_*Ace 52 python http urllib2
我试图通过在Python中编写登录序列脚本来测试Web应用程序的功能,但我遇到了一些麻烦.
这是我需要做的事情:
现在,我对python相对较新,但到目前为止我测试过的两件事都没有用.首先我使用了httplib,putrequest()(传递URL中的参数)和putheader().这似乎没有遵循重定向.
然后我尝试了urllib和urllib2,将标题和参数作为dicts传递.这似乎返回登录页面,而不是我尝试登录的页面,我想这是因为缺少cookie或其他东西.
我错过了一些简单的事吗?
谢谢.
S.L*_*ott 31
专注urllib2
于此,它运作良好.不要乱用httplib
,它不是顶级API.
您注意到的是urllib2
不遵循重定向.
您需要折叠一个实例HTTPRedirectHandler
,以捕获并遵循重定向.
此外,您可能希望将默认值子类HTTPRedirectHandler
化为捕获信息,然后将其作为单元测试的一部分进行检查.
cookie_handler= urllib2.HTTPCookieProcessor( self.cookies )
redirect_handler= HTTPRedirectHandler()
opener = urllib2.build_opener(redirect_handler,cookie_handler)
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用此opener
对象进行POST和GET,正确处理重定向和cookie.
您可能还想添加自己的子类HTTPHandler
来捕获和记录各种错误代码.
Jas*_*pas 15
这是我对这个问题的看法.
#!/usr/bin/env python
import urllib
import urllib2
class HttpBot:
"""an HttpBot represents one browser session, with cookies."""
def __init__(self):
cookie_handler= urllib2.HTTPCookieProcessor()
redirect_handler= urllib2.HTTPRedirectHandler()
self._opener = urllib2.build_opener(redirect_handler, cookie_handler)
def GET(self, url):
return self._opener.open(url).read()
def POST(self, url, parameters):
return self._opener.open(url, urllib.urlencode(parameters)).read()
if __name__ == "__main__":
bot = HttpBot()
ignored_html = bot.POST('https://example.com/authenticator', {'passwd':'foo'})
print bot.GET('https://example.com/interesting/content')
ignored_html = bot.POST('https://example.com/deauthenticator',{})
Run Code Online (Sandbox Code Playgroud)
Ace*_*Ace 13
@ S.Lott,谢谢.你的建议对我有用,经过一些修改.这就是我做到的.
data = urllib.urlencode(params)
url = host+page
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request)
cookies = CookieJar()
cookies.extract_cookies(response,request)
cookie_handler= urllib2.HTTPCookieProcessor( cookies )
redirect_handler= HTTPRedirectHandler()
opener = urllib2.build_opener(redirect_handler,cookie_handler)
response = opener.open(request)
Run Code Online (Sandbox Code Playgroud)
Eli*_*ght 11
我最近必须自己做这件事.我只需要标准库中的类.这是我的代码的摘录:
from urllib import urlencode
from urllib2 import urlopen, Request
# encode my POST parameters for the login page
login_qs = urlencode( [("username",USERNAME), ("password",PASSWORD)] )
# extract my session id by loading a page from the site
set_cookie = urlopen(URL_BASE).headers.getheader("Set-Cookie")
sess_id = set_cookie[set_cookie.index("=")+1:set_cookie.index(";")]
# construct headers dictionary using the session id
headers = {"Cookie": "session_id="+sess_id}
# perform login and make sure it worked
if "Announcements:" not in urlopen(Request(URL_BASE+"login",headers=headers), login_qs).read():
print "Didn't log in properly"
exit(1)
# here's the function I used after this for loading pages
def download(page=""):
return urlopen(Request(URL_BASE+page, headers=headers)).read()
# for example:
print download(URL_BASE + "config")
Run Code Online (Sandbox Code Playgroud)
尝试使用twill - 一种允许用户从命令行界面浏览Web的简单语言.使用斜纹,您可以浏览使用表单,cookie和大多数标准Web功能的网站.更重要的是,twill被编写Python
并具有python API,例如:
from twill import get_browser
b = get_browser()
b.go("http://www.python.org/")
b.showforms()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30770 次 |
最近记录: |