欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

urllib2 get post 博客分类: ptython python 

程序员文章站 2024-03-13 10:00:39
...
python post,get 的 方法

post  #参数有 data 就是 post,没有data 是 get
1
url="https://api.ext.m-m10010.com/open/unicom/BatchQueryTerminal"
data = {"userId":MLB_USERID,"num":imsis,"num_type":"imsi","timestamp":dt,"sign":token}
req = urllib2.Request(url)
req.add_header("Content-Type","application/json") #有些需要 headers 不然会失败
res = urllib2.urlopen(req,json.dumps(data))
print res.read()

2
headers = {"Content-Type":"application/json"}
req = urllib2.Request(
             url,
             json.dumps(data),
             headers
        )

res =  urllib2.urlopen(req)
prin res.read()

3    # 不需要headers
res = urllib2.urlopen(
            url = url,
            data = json.dumps(data),
            timeout = 5
        )
print  res.read()

4  #需要urlencode
data = urllib.urlencode(data)
res = urllib2.urlopen(url=url,data=data,timeout=5)



相关标签: python