python接口自动化测试——上传图片举例

时间:2024-02-18 10:09:15

利用抓包登陆后获取cookie,然后进行图片上传 携带文件名称和cookie



import requests

\'\'\'
人人网登陆,图片上传
\'\'\'
def loginData():
\'\'\'登录请求参数\'\'\'
data = {
\'email\': \'13484545195\',
\'icode\': \'\',
\'origURL\': \'http://www.renren.com/home\',
\'domain\': \'renren.com\',
\'key_id\': 1,
\'captcha_type\': \'web_login\',
\'password\': \'8d9a71152919613bbe3df9bfa0e1b390eb2b13dd1bdde270c2816cf04dd1b7c5\',
\'rkey\': \'b4cdc6acc1d36171e3de73dd4676208e\',
\'f\': \'http%3A%2F%2Fname.renren.com%2F\'}
return data

def login():
\'\'\'登陆,获取cookie\'\'\'
r = requests.post(
url=\'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201894216799\',
data=loginData(),
headers={\'Content-Type\': \'application/x-www-form-urlencoded\'}
)
return r.cookies


def getData():
\'\'\'上传文件请求参数\'\'\'
data = {
"upload":"提交",
"__channel":"renren",
"privacyParams":"{\'sourceControl\': 99}",
"hostid":"967004081",
"requestToken":"-1996788973",
"_rtk":"54bc696dv "
}
return data


def upload():
\'\'\'上传\'\'\'
r = requests.post(
url=\'http://upload.renren.com/upload.fcgi?pagetype=addpublishersingle&hostid=967004081&\'
\'callback=window.parent.handlePhotoData&uploadid=profile_publisher_photo_1540215890321\',
data=getData(),
headers={\'Conteny-Type\': \'multipart/form-data\' },
    \'\'\'文件上传的重点参数 files\'\'\'
files={"file": ("wx.jpg", open("d:/xx.jpg", "rb"), "image/jpeg", {})},
cookies=login())
print(r.status_code)
print(r.text)

upload()