由于现在微服务,爬虫等在项目中的大量需求,所以python关于Get Post方法的实现也就作为基础一直被大量使用。这里给出基于urllib,urllib2的get、post示例。
get请求
get_url = u'https://simple.xxxx.com/getJsonData?version=%s&sort=1'%model['version']
req = urllib2.Request(url)
res_data = urllib2.urlopen(req)
res = res_data.read()
post请求
post_url = u'https://imple.xxxx.com/getJsonData'
post_data = {'version': model['version'], 'sort':1}
post_data_urlencode = urllib.urlencode(post_data)
req = urllib2.Request(url=post_url, data=post_data_urlencode)
res_data = urllib2.urlopen(req)
res = res_data.read()
对比两者的区别,主要在对于参数的处理上。当然更深的可以处理header,加密等其他事情。。。