若不适用cookies,发送http post
import urllib2
import urllib
data={'name':'www','password':'123456'}
f=urllib2.urlopen(
url='http://www.ideawu.net/',
data=urllib.urlencode(data)
)
print f.read()
若使用cookies ,发送http post
import urllib2
cookies=urllib2.HTTPCookieProcessor()
opener=urllib2.build_opener(cookies)
f=opener.open('http://www.ideawu.net/?act=login&name=user01')
data ='<root>hello</root>'
request=urllib2.Request(
url ='http://www.ideawu.net/?act=send',
headers={'content-Type':'text/xml'},
data=data
)
opener.open(request)
第一次使用open(),是进行登录,服务器返回cookies自动保存到cookies中
第二次使用open(),使用post方法向服务器发送content-Type=text/xml数据。如果不使用request方法,python会强制使用urlencode,将把content-type改为application/x-www-form-urlencoded