码迷,mamicode.com
首页 > 编程语言 > 详细

python接口测试(二)

时间:2016-04-05 14:12:49      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:python   request   

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

python接口测试(二)

标签:python   request   

原文地址:http://cqtesting.blog.51cto.com/8685091/1760383

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!