您的位置:首页 > 编程语言 > Python开发

python perl curl 模拟get请求

2017-10-18 09:13 295 查看
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:\Python27\python.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)



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: