Focus on urllib2
for this, it works quite well. Don't mess with httplib
, it's not the top-level API.
What you're noting is that urllib2
doesn't follow the redirect.
You need to fold in an instance of HTTPRedirectHandler
that will catch and follow the redirects.
Further, you may want to subclass the default HTTPRedirectHandler
to capture information that you'll then check as part of your unit testing.
cookie_handler= urllib2.HTTPCookieProcessor( self.cookies )
redirect_handler= HTTPRedirectHandler()
opener = urllib2.build_opener(redirect_handler,cookie_handler)
You can then use this opener
object to POST and GET, handling redirects and cookies properly.
You may want to add your own subclass of HTTPHandler
to capture and log various error codes, also.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…