• curl perl python get请求带请求头


    curl get请求带请求头:
    curl -H "Authorization: Token 9ec341da308a60047b72f9766b5b98d0d819562c" -H 'Accept: application/json; indent=4' http://http://192.168.137.1:8000/api2/accounts/
    
      -H/--header <header>
    
     (HTTP) 额外的header 来使用当getting一个web page.你可以指定任意数量的额外的headers.
    
      注意如果你增加一个自定义的header,和内部有相同的名字,你外部设置header会代替内部internal.
    node2:/root#curl -H "Authorization: Token 9ec341da308a60047b72f9766b5b98d0d819562c" -H 'Accept: application/json; indent=4' http://192.168.137.1:8000/api2/accounts/
    [
        {
            "source": "DB",
            "email": "015208@zjtlcb.com"
        },
        {
            "source": "DB",
            "email": "99999@zjtlcb.com"
        },
        {
            "source": "DB",
            "email": "zhaoyangjian@gmail.com"
        },
        {
            "source": "DB",
            "email": "zhangzhong@gmail.com"
        },
        {
            "source": "DB",
            "email": "zhouhaiwu@gmail.com"
        }
    ]node2:/root#
    
    perl 模拟get发送请求头
    
    
    use  LWP::UserAgent; 
    use LWP;
    use Encode;
    use LWP::Simple;
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use URI::Escape;
    use URI::URL;
    use JSON;
    use Data::Dumper;
      my $ua = LWP::UserAgent->new;
      $ua->agent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0");
      my $cookie_jar = HTTP::Cookies->new(
         file=>'lwp_cookies.txt',
         autosave=>1,
         ignore_discard=>1);
         $ua->cookie_jar($cookie_jar);
       my $token_url= 'http://127.0.0.1:8000/api2/auth-token/';
                    
      
       my $res = $ua->post($token_url,
                    {
                    'username'=>'015208@zjtlcb.com',
                    'password'=>'1234567'
                    });
       print $res->content();
       print "
    ";
       my $r= $res->content();
       my $r=encode_utf8($r);
       my $hash = decode_json($r);
       my $token =$hash->{"token"};
       print "$r is $r
    ";
       my $host = "http://127.0.0.1:8000/api2/auth/ping/";
       @header = (  
        'accept'=> "application/json",  
        'content-type'=> "application/json",  
        'Authorization'=> "Token $token" 
        );  
        
        $host="http://127.0.0.1:8000/api2/accounts/";  
        $request = HTTP::Request->new(GET=>"$host");  
        $request->header(@header);  
      
        $response = $ua->request($request);   
        print $response->decoded_content;  
     
        my $r=$response->decoded_content;
    	my $r=encode_utf8($r);
        my $arr = decode_json($r);
    	print "---------------------------
    ";
    	foreach $a (@{$arr}){
    	  print $a->{"email"};
    	  print "
    ";
    	  };
    
    C:UsersTLCBDesktoppythonPython 高级编程4>perl a2.pl
    {"token": "9ec341da308a60047b72f9766b5b98d0d819562c"}
    $r is {"token": "9ec341da308a60047b72f9766b5b98d0d819562c"}
    [{"source": "DB", "email": "015208@zjtlcb.com"}, {"source": "DB", "email": "9999
    9@zjtlcb.com"}, {"source": "DB", "email": "zhaoyangjian@gmail.com"}, {"source":
    "DB", "email": "zhangzhong@gmail.com"}, {"source": "DB", "email": "zhouhaiwu@gma
    il.com"}]---------------------------
    015208@zjtlcb.com
    99999@zjtlcb.com
    zhaoyangjian@gmail.com
    zhangzhong@gmail.com
    zhouhaiwu@gmail.com
    
    
    python 模拟get 发送自定义请求头
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import urllib2
    import urllib
    import cookielib
    import json
    import httplib
    
    
    def gettoken():
         data = {'username': '99999@zjtlcb.com', 'password': '1234567'}
         post_data = urllib.urlencode(data)  # 将post消息化成可以让服务器编码的方式
         cj = cookielib.CookieJar()  # 获取cookiejar实例
         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
         # 自己设置User-Agent(可用于伪造获取,防止某些网站防ip注入)
         headers = {}
         website = "http://127.0.0.1:8000/api2/auth-token/"
         req = urllib2.Request(website, post_data, headers)
         content = opener.open(req)
         s = content.read()  # linux下没有gbk编码,只有utf-8编码
         print s
         print type(s)
         text = json.loads(s)
         print type(text)
         print text['token']
         token = text['token']
         return token
    
    
    def list_account():
        token = gettoken()
        print len(token)
        token = 'Token' + ' ' + token
        print token
        url = 'http://127.0.0.1:8000/api2/accounts/'
        conn = httplib.HTTPConnection('127.0.0.1', 8000)
        headers = {"Authorization": token, "Accept": "application/json; indent=10", "content-type": "application/json"}
        print headers
        conn.request('GET', url, '', headers)
        response = conn.getresponse()
        res = response.read()
        print res
    list_account()
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a4.py
    {"token": "0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7"}
    <type 'str'>
    <type 'dict'>
    0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7
    40
    Token 0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7
    {'content-type': 'application/json', 'Accept': 'application/json; indent=10', 'Authorization': u'Token 0ac9e8585ef6ae51eb62c785d10a6c5102de3ff7'}
    [
            {
                    "source": "DB",
                    "email": "015208@zjtlcb.com"
            },
            {
                    "source": "DB",
                    "email": "99999@zjtlcb.com"
            },
            {
                    "source": "DB",
                    "email": "zhaoyangjian@gmail.com"
            },
            {
                    "source": "DB",
                    "email": "zhangzhong@gmail.com"
            },
            {
                    "source": "DB",
                    "email": "zhouhaiwu@gmail.com"
            }
    ]
    
    Process finished with exit code 0

  • 相关阅读:
    wifi通信过程的研究--(2)Wifi传输认证过程
    wifi通信过程的研究--(1)Wifi基本属性介绍
    wifi通信过程的研究--(3)传输过程概念细分
    网络编程之TCP/IP各层详解
    持续集成CI与自动化构建
    IEEE 802.11标准列表
    IEEE802.11协议基础知识
    IEEE 802.11协议基础知识整理
    beacon帧字段结构最全总结(三)——VHT字段总结
    beacon帧字段结构最全总结(二)——HT字段总结
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349511.html
Copyright © 2020-2023  润新知