Python接口测试的笔记

#!/usr/bin/env python
# coding: utf-8 -*-
import urllib,urllib2,requests
import re,time,os,sys
import threading,json

# ####### get请求 ############
# r = requests.get('https://github.com/timeline.json')
# get输入params关键字参数
# payload = {'key1': 'value1', 'key2': 'value2'}
# payload = {'key1': 'value1', 'key2': ['value2', 'value3']}  # key2=value2&key2=value3
# r = requests.get("http://httpbin.org/get", params=payload)
# print r.url
# print r.text
# print r.json()

# ####### post请求 ############
url = 'https://api.github.com/some/endpoint'
requests.get('http://github.com', timeout=0.001)
headers = {'user-agent': 'my-app/0.0.1'}
# 传入的字典:
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url,headers=headers,data=payload)
# 传入的字符串:
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload))
# 使用json参数传递:
payload = {'some': 'data'}
r = requests.post(url, json=payload)
# 输入raw
payload = "{'NickName': \'风度\'}"

# ######### 响应 ####33
r.status_code == requests.codes.ok
print r.text
print r.status_code
print r.headers['Content-Type']
print r.cookies['example_cookie_name']
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)

# #########调用多个json
a = json.loads(r.text)
b = [a[u'access_token'], a[u'cookies']]
return b
print achieve()[0], achieve()[1]  #achieve为定义def容器

# ####去除u'' ######
newtemp = str(temp).replace('u\'', '\'')
newtemp.decode("unicode-escape")

# 传参:
data1 = '''{'BackLogID': "''' + test_QueryBacklogs()[0] + '''"}'''

# 传入的字符串:
data = "{'NickName': \'win\'}"

x='林'
print x.decode('utf-8')

unicode 转为 gb2312,utf-8等,使用 encode(encoding)
s = u'中国'
s_gb = s.encode('utf-8')

utf-8,GBK转换为 unicode
1. unicode(s,utf-8)
2. s.decode(utf-8)

temp = json.dumps(r.text)
temp = json.loads(r.text)

猜你喜欢

转载自blog.csdn.net/qq_28905427/article/details/80729717