python用httplib模块发送get和post请求***

在python中,模拟http客户端发送get和post请求,主要用httplib模块的功能。

1、python发送GET请求

我在本地建立一个测试环境,python.php的内容就是输出一句话:

php
echo 'python httplib study!'.PHP_EOL;
var_dump($_POST);
?>

python发送get请求代码:

import httplib

httpClient = None

try:
    httpClient = httplib.HTTPConnection('localhost', 80, timeout=10)
    httpClient.request('GET', '/python.php')

    # response是HTTPResponse对象
    response = httpClient.getresponse()
    print response.status
    print response.reason
    print response.read()
except Exception, e:
    print e
finally:
    if httpClient:
        httpClient.close()

上面代码中使用了finally来保证即使出错的时候也能关闭httpClient。运行这个程序,在我的电脑上输出结果如下:

2、python发送POST请求

修改python.php内容,打印出$_POST数组:

php
echo 'python httplib study!'.PHP_EOL;
var_dump($_POST);
?>

python发起post请求代码:

#!/usr/bin/env python
# -*- coding: gbk -*-
# -*- coding: utf-8 -*-
import urllib, httplib

httpClient = None
try:
    params = urllib.urlencode({'name': 'waitalone.cn', 'age': '5'})
    headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'}
    httpClient = httplib.HTTPConnection('localhost', 80, timeout=10)
    httpClient.request('POST', '/python.php', params, headers)
    response = httpClient.getresponse()
    print response.status
    print response.reason
    print response.read()
    print response.getheaders()
except Exception, e:
    print e
finally:
    if httpClient:
        httpClient.close()

 

 

Python3处理HTTP请求

Python3处理HTTP请求的包:http.client,urllib,urllib3,requests
其中,http 比较 low-level,一般不直接使用
urllib更 high-level一点,属于标准库。urllib3跟urllib类似,拥有一些重要特性而且易于使用,但是属于扩展库,需要安装
requests 基于urllib3 ,也不是标准库,但是使用非常方便

# import http.client
# http_client = http.client.HTTPConnection('localhost',8080,timeout=10)
# http_client.request('get','/jenkins/api/json?pretty=true')
# response = http_client.getresponse()
# print(response.status)
# print(response.read())

# import urllib.request
# response = urllib.request.urlopen('http://localhost:8080/jenkins/api/json?pretty=true')
# print(response.status)
# print(response.read())

# import urllib3
# response = urllib3.PoolManager().request('get','http://localhost:8080/jenkins/api/json?pretty=true')
# print(response.status)

# import  requests
# response = requests.get('http://localhost:8080/jenkins/api/json?pretty=true')
# print(response.status_code)
# print(response.text)
# print(response.json())
# print(response.reason)
import requests
from requests.auth import HTTPBasicAuth
response = requests.post('http://localhost:8080/jenkins/job/check_python_version/build',auth=('admin','wangmin'))
print (response.status_code)
print (response.reason)
print(response.headers)

JSON

import urllib.request
import json
   
if __name__ == "__main__":
    restUri = "https://lxxx";
    PostParam = "data=123456"
    DATA = PostParam.encode('utf8')
    req = urllib.request.Request(url = restUri, data=DATA, method='POST')
    req.add_header('Content-type', 'application/x-www-form-urlencoded')
    r = urllib.request.urlopen(req).read()
    print(r.decode('utf8'))
    org_obj = json.loads(r.decode('utf8'))
    print(org_obj['token'])

 

你可能感兴趣的:(python用httplib模块发送get和post请求***)