urllib2库做接口测试
GET请求
# coding = utf-8import urllib2response = urllib2.urlopen('http://localhost:8080/jenkins/api/json')print response.read()
POST请求
import urllib2import urllibpost_data = urllib.urlencode({}) #传入POST请求所带的参数,此处若不需要传入参数则为空response = urllib2.urlopen('http://localhost:8080/jenkins/job/check_python_version/polling', post_data)print response.read()print response.getheaders()
403报错,没有权限操作,引入第三方库Requests:
安装Requests:
pip install requests -i http://pypi.douban.com/simple/
E:\py>pip install requests -i http://pypi.douban.com/simple/
Downloading/unpacking requests
http://pypi.douban.com/simple/requests/ uses an insecure transport scheme (http). Consider using https if pypi.douban.com has it available
Installing collected packages: requests
Successfully installed requests
Cleaning up...
Requests
GET请求
#coding=utf-8import requestsprint requests.get('http://localhost:8080/jenkins/api/json').text
POST请求
#coding=utf-8import requestsurl = 'http://localhost:8080/jenkins/job/check_python_version/polling'r = requests.post(url, data={}, auth=('admin', '123456'))print r.status_codeprint r.headersprint r.reason