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接口调用
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

over!

 
posted @ 2018-06-05 14:31  北京流浪儿  阅读(633)  评论(0编辑  收藏  举报