zoukankan      html  css  js  c++  java
  • 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!

     
  • 相关阅读:
    CSS深入之第四天
    CSS之第三天总结
    第二天对CSS的学习
    开始走进CSS世界
    Hbuilder实用技巧
    项目总结
    CSS3的chapter6
    CSS3的chapter5
    CSS3的chapter4
    CSS3的chapter3
  • 原文地址:https://www.cnblogs.com/gide/p/9139641.html
Copyright © 2011-2022 走看看