Python模拟HTTP的POST请求

Python版本2.7.9

模拟POST请求

 1 #coding:u8
 2 import urllib
 3 import urllib2
 4 
 5 url = u"http://192.168.84.182:8000/Home/GetTheme"
 6 params = urllib.urlencode({'name':'tom','age':22})
 7 
 8 req = urllib2.Request(url,params)
 9 response = urllib2.urlopen(req)
10 the_page = response.read()
11 print the_page

使用urllib2来模拟post请求时,参数先使用字典表示,在使用urllib.urlencode进行编码,该方法只支持这种键值对类型

补充:urllib与urllib2并不是2是1的升级版,两个库都是项目独立的,不要误解了。

posted @ 2015-08-13 16:12  CodeTracker  阅读(601)  评论(0编辑  收藏  举报