python 如何通过POST上传EXCEL文件到网站,不懂如何写,抓包软件抓到RAW信息如下:
POST http://10.184.19.2:8000/api/tc-config-biz/FileOperateController/uploadFiles HTTP/1.1
Host: 10.184.19.2:8000
Authorization: Bearer 580131a9-30c9-4b39-9d82-c1af8341eeb5
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary26TicDu5Peqo2sb7
Accept: /
Origin: http://10.184.19.2:8000/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
------WebKitFormBoundary26TicDu5Peqo2sb7
Content-Disposition: form-data; name="attachment"
{"url":"sheet/gt/","bucketName":"nctc"}
------WebKitFormBoundary26TicDu5Peqo2sb7
Content-Disposition: form-data; name="context"
{"userUuid":"pangyou","userName":"pangyou","userIp":"","serverId":"","metaUuid":""}
------WebKitFormBoundary26TicDu5Peqo2sb7
Content-Disposition: form-data; name="file"; filename="产品使用情况.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
```python
header = {
'Authorization':'bearer 580131a9-30c9-4b39-9d82-c1af8341eeb5',
'Connection': 'keep-alive',
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary26TicDu5Peqo2sb7',
'Host': '10.184.19.2:8000',
'Origin': 'http://10.184.19.2:8000',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
#上传文件
url='http://10.184.19.2:8000/api/tc-config-biz/FileOperateController/uploadFiles'
filename='产品使用情况.xlsx'
filepath='D:/pycharm/yunwei_ceshi/产品使用情况.xlsx'
body={
'bucketName':"nctc",
"url":"sheet/gt/"
}
with open(filepath,'rb') as f_:
m= MultipartEncoder(
fields={
"userUuid": "pangyou",
"userName": "pangyou",
"userIp": "",
"serverId": "", "metaUuid": "",
'file':(filename,f_,'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')}
)
response=requests.post(url,headers=header,json=body,data=m,timeout=None,verify=False)
print(m.content_type)
print (response.text)
```
用requests库来上传文件。例子,根据情况调整:
这里有个注意事项:requests库中,提供了MultipartEncoder
类来处理multipart/form-data类型的请求,这个类是用来处理要上传的文件和其他数据。并且会自动生成boundary。
用'Content-Type': m.content_type
在header中设置这个自动生成的boundary,而不是手动设置。
在你的情况中,以下是修改后的代码:
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
url='http://10.184.19.2:8000/api/tc-config-biz/FileOperateController/uploadFiles'
filename='产品使用情况.xlsx'
filepath='D:/pycharm/yunwei_ceshi/产品使用情况.xlsx'
header = {
'Authorization':'Bearer 580131a9-30c9-4b39-9d82-c1af8341eeb5',
'Connection': 'keep-alive',
'Host': '10.184.19.2:8000',
'Origin': 'http://10.184.19.2:8000',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
with open(filepath,'rb') as f_:
m = MultipartEncoder(
fields={
"attachment": '{"url":"sheet/gt/","bucketName":"nctc"}',
"context": '{"userUuid":"pangyou","userName":"pangyou","userIp":"","serverId":"","metaUuid":""}',
'file':(filename,f_,'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')}
)
header['Content-Type'] = m.content_type # 增加一行来设置boundary
response=requests.post(url,headers=header,data=m,timeout=None,verify=False)
print (response.text)
上述代码将文件数据和其他数据一并通过MultipartEncoder
打包,并且在header中设置了正确的'Content-Type'。
你要确保你的requests库和requests_toolbelt库是最新的。以下命令更新:
pip install --upgrade requests
pip install --upgrade requests_toolbelt
你可以按照上述方式试试看是否可以解决你的问题。
我有写好的demo,还可以写入到excel,稍后我发你看看,我是用html页面发起请求上传到网站并保存文件的。
看你的意思是用python代码直接上传就行?服务端的代码你是已经有了吗?
我给你整理个简单的demo示例吧:
客户端上传代码:
import requests
url = "http://10.184.19.2:8000/api/tc-config-biz/FileOperateController/uploadFile" # 替换为目标网站的上传接口 URL
file_path = "D:/pycharm/yunwei_ceshi/产品使用情况.xlsx" # 替换为要上传的 Excel 文件路径
# 打开并读取 Excel 文件内容
with open(file_path, 'rb') as file:
file_data = file.read()
# 发送 POST 请求,并携带文件数据
response = requests.post(url, files={"file": file_data})
# 检查响应状态码,200 表示请求成功
if response.status_code == 200:
print("文件上传成功")
else:
print("文件上传失败")
服务端接收代码:
from flask import Flask, request
app = Flask(__name__)
@app.route('/uploadFile', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return 'No file found', 400
file = request.files['file']
if file.filename == '':
return 'No selected file', 400
# 保存文件到指定路径
file.save('path/to/save/file.xlsx')
return 'File uploaded successfully', 200
if __name__ == '__main__':
app.run(host="0.0.0.0",port=8000)
您可以根据需要调整保存的路径和文件名。
采用chatgpt:
要通过POST上传EXCEL文件到网站,你需要使用requests库,并在请求头中设置正确的Content-Type和boundary。另外,你还需要使用MultipartEncoder来构建multipart/form-data类型的请求体。
下面是修改后的代码:
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
url = 'http://10.184.19.2:8000/api/tc-config-biz/FileOperateController/uploadFiles'
filename = '产品使用情况.xlsx'
filepath = 'D:/pycharm/yunwei_ceshi/产品使用情况.xlsx'
header = {
'Authorization': 'Bearer 580131a9-30c9-4b39-9d82-c1af8341eeb5',
'Host': '10.184.19.2:8000',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary26TicDu5Peqo2sb7',
'Origin': 'http://10.184.19.2:8000/',
}
body = {
'bucketName': 'nctc',
'url': 'sheet/gt/'
}
with open(filepath, 'rb') as f_:
m = MultipartEncoder(
fields={
'attachment': '{"url": "sheet/gt/", "bucketName": "nctc"}',
'context': '{"userUuid": "pangyou", "userName": "pangyou", "userIp": "", "serverId": "", "metaUuid": ""}',
'file': (filename, f_, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
}
)
header['Content-Type'] = m.content_type
response = requests.post(url, headers=header, data=m.to_string(), verify=False)
print(response.text)
这里使用了requests_toolbelt库中的MultipartEncoder来构建请求体,确保正确设置Content-Type。请确保你已经安装了该库,可以使用pip install requests-toolbelt命令进行安装。
修改后的代码假设你已经安装了相应的库,并且请求的URL、文件路径、请求头等信息都是正确的。另外,verify=False用于跳过SSL证书验证,如果你的网站使用了有效的SSL证书,请去掉该参数或设置为True。