python接口测试(二)

urllib2库做接口测试

GET请求

1
2
3
4
# coding = utf-8
import urllib2
response = urllib2.urlopen('http://localhost:8080/jenkins/api/json')
print response.read()

POST请求

1
2
3
4
5
6
7
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请求

1
2
3
#coding=utf-8
import requests
print requests.get('http://localhost:8080/jenkins/api/json').text

POST请求

1
2
3
4
5
6
7
#coding=utf-8
import requests
url = 'http://localhost:8080/jenkins/job/check_python_version/polling'
= requests.post(url, data={}, auth=('admin''123456'))
print r.status_code
print r.headers
print r.reason
















本文转自cqtesting51CTO博客,原文链接:http://blog.51cto.com/cqtesting/1760383 ,如需转载请自行联系原作者

上一篇:HBase MemStore Flush 机制时机


下一篇:如何让百度收录你的GitHub Pages博客