python的get方式提交请求

post是“送”,get是“取”,为什么能通过“get”方式“提交”数据呢?get是将数据信息放在了url中,取的同时,进行了送。在不使用httplib时,代码风格上并没有本质区别。区别为:

  • post请求:
    使用post方式时,数据放在data或者body中,不能放在url中,放在url中将被忽略。
  • get请求:
    使用get方式时,请求数据直接放在url中。
    来个例子。
import urllib
import urllib2
url = "http://appt.igeekery.com/wx-run/set-step.json?_lan=zh&_k=da3e20cc7e524a5a\
b22360372f564875&_v=36&_ch=16&_nw=wifi&_sdk=19&_product=fitmix&_terminal=2&uid=458009&uni\
onid=oIoQTsy2pk-LKb4hnq0EK9m_3QC4&openid=oBmGhuPxQVTPVBfZyWhskgT4TTos&step\
=15890"
print url
req = urllib2.Request(url)
print req
res_data = urllib2.urlopen(req)
print res_data
res = res_data.read()
print res

参考1
参考2

注意:urllib2.Request创建一个Request对象,此时还未提交请求。接着通过调用urlopen并传入Request对象,将返回一个相关请求response对象,这个应答对象如同一个文件对象,所以你可以在Response中调用.read()。关键交互步骤为urllib2.urlopen。

你可能感兴趣的:(前端与python)