Python调用HTTP接口并传递cookie
get接口调用
import urllib
import urllib2
get_url = "http://10.10.3.63/test?id=123&name=nba"
cookie_headers = {
"Cookie" : "person_id=2468"
}
req = urllib2.Request(url=get_url,headers=cookie_headers)
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
post接口调用
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import urllib
import urllib2
args_data = {
'id':'321',
'name':'cba'
}
args_data_urlencode = urllib.urlencode(args_data)
post_url = "http://10.10.3.63/test"
cookie_headers = {
"Cookie" : "person_id=24681111;bureau_id=0000"
}
req = urllib2.Request(url = post_url, data = args_data_urlencode, headers=cookie_headers)
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
requests调用
import requests
import json
args_data = {
'id':'321',
'name':'cba'
}
pdata = json.dumps(args_data)
headers = {'Content-Type': 'application/json',"Cookie":"person_id=24681111;bureau_id=0000"}
res = requests.post(url, data=pdata, headers=headers)
print res.text