1)Redirect will managed bu urllib2 Httpredirecthandler automatically,we don't need any code to handle it,one thing need to remember that 307 with POST is not support by urllib,need to change the library
2)session can also be managed by module automatically,the only thing we need to do is add urllib2.HTTPCookieProcessor() while build opener
3)we need create HTTPBasicAuthHandler with a HTTPPasswordMgrWithDefaultRealm that need add username and password
here is the sample code that can handle redirection,Basic Authentication,session management(cookie processor)
4) when we add password to passwordmanagement,we need give the url of the server,not the url we point to to fetch data
5)cmd is the data we send in http message body.
server = 'http://172.19.xxx.xxx/'
url = server + 'XmlConfig'
username='guest'
password ='xxxxxxxxxxxx'
cmd ='
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, server, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler,urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)
response = urllib2.urlopen(url,cmd)
thepage = response.read()
print thepage
No comments:
Post a Comment