urllib2库做接口测试
GET请求
# coding = utf-8 import urllib2 response = urllib2.urlopen(‘http://localhost:8080/jenkins/api/json‘) print response.read()
POST请求
import urllib2 import urllib post_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-8 import requests print requests.get(‘http://localhost:8080/jenkins/api/json‘).text
POST请求
#coding=utf-8 import requests url = ‘http://localhost:8080/jenkins/job/check_python_version/polling‘ r = requests.post(url, data={}, auth=(‘admin‘, ‘123456‘)) print r.status_code print r.headers print r.reason
本文出自 “on_the_road” 博客,请务必保留此出处http://cqtesting.blog.51cto.com/8685091/1760383
原文地址:http://cqtesting.blog.51cto.com/8685091/1760383