urllib2 get post

阅读更多
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)