zoukankan      html  css  js  c++  java
  • python perl curl 模拟get请求

    curl:
    
    curl http://192.168.137.1:8000/api2/ping/
    
    node2:/root#curl http://192.168.137.1:8000/api2/ping/
    "pong"node2:/root#
    
    
    
    perl 版本:
    
    use  LWP::UserAgent; 
    my $ua = LWP::UserAgent->new;
    $ua->timeout(10);
    $ua->env_proxy;
    $ua->agent("Mozilla/8.0");
    my $host = "http://192.168.137.1:8000/api2/ping/";
    my $response = $ua->get($host);
       $ua->default_headers;
      if ($response->is_success) {
          print $response->decoded_content;  # or whatever
       }
        else {
        die $response->status_line;
    }
    
    
    python 版本:
    
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import urllib2
    import urllib
    import cookielib
    import json
    import httplib
    url = 'http://192.168.137.1:8000/api2/ping/'
    conn = httplib.HTTPConnection('192.168.137.1', 8000)
    header={}
    conn.request('GET', url, '', header)
    response = conn.getresponse()
    res = response.read()
    print res
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a1.py
    "pong"
    
    import json
    import urllib
    import urllib2
    url = "https://www.zjcap.cn/web/noauth?productSn=1079&method=%2Fproduct%2Fdetail&platform=web&_=1509514340879"
    req = urllib2.Request(url)
    print req
    print type(req)
    
    res_data = urllib2.urlopen(req)
    res = res_data.read()
    print res
    print type(res)
    


    
                                        
    
  • 相关阅读:
    软工实践寒假作业(1/2)
    java判断是否为数字
    前端测试工具Cypress
    StringBuffer&StringBuilder
    IO流
    kafka简介
    Python学习笔记10--unittest参数化
    python学习笔记9--日志模块logging
    Python学习笔记9-多线程和多进程
    python学习笔记9-单元测试unittest
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349512.html
Copyright © 2011-2022 走看看