python request.post圖片和字典

純潔的程式碼發表於2020-02-05

#coding:utf-8

import requests
import os
import json
from requests_toolbelt.multipart.encoder import MultipartEncoder
# from my_compat import MultipartEncoder
url = ''
#
圖片路徑
img_path ="./images/"
files_name = os.listdir(img_path) #
得到資料夾下的所有檔名稱
image0=open("./images/"+files_name[0], "rb")
image1=open("./images/"+files_name[1], "rb")
# print("fck===",image0)
# image0 =(files_name[0], open("./images/"+files_name[0], "rb"), "image/jpeg")
files = {'file': (files_name[0], open("./images/"+files_name[0], "rb"), "image/jpeg")}
# print("files===",files)
function(){ //MT4買賣價 http://www.fx61.com/faq/muniu/436.html
imagelist=[]
imagelist.append(image0)
# imagelist.append(image1)
# imagelist=os.listdir(img_path)
# print("fck===",imagelist)
# payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"; filename=\"\\home\\edgeai\\Projects\\test_post\\images\\download.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"json\"\r\n\r\n{\n\t\"cId\": 1,\n\t\"trackList\": [{\n\t\t\"frameList\": [\n\t\t\t{\n\t\t\t\"frameIndex\": 1571830178957,\n\t\t\t\"imageIndex\": 0,\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 574,\n\t\t\t\t\"x\": 818,\n\t\t\t\t\"w\": 105,\n\t\t\t\t\"h\": 210\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179006,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 575,\n\t\t\t\t\"x\": 818,\n\t\t\t\t\"w\": 109,\n\t\t\t\t\"h\": 213\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179044,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 584,\n\t\t\t\t\"x\": 832,\n\t\t\t\t\"w\": 112,\n\t\t\t\t\"h\": 213\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179086,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 581,\n\t\t\t\t\"x\": 852,\n\t\t\t\t\"w\": 115,\n\t\t\t\t\"h\": 209\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179131,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 583,\n\t\t\t\t\"x\": 869,\n\t\t\t\t\"w\": 117,\n\t\t\t\t\"h\": 204\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179168,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 590,\n\t\t\t\t\"x\": 880,\n\t\t\t\t\"w\": 119,\n\t\t\t\t\"h\": 203\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179207,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 596,\n\t\t\t\t\"x\": 888,\n\t\t\t\t\"w\": 120,\n\t\t\t\t\"h\": 200\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179240,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 600,\n\t\t\t\t\"x\": 894,\n\t\t\t\t\"w\": 119,\n\t\t\t\t\"h\": 200\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179288,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 600,\n\t\t\t\t\"x\": 899,\n\t\t\t\t\"w\": 117,\n\t\t\t\t\"h\": 199\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179326,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 604,\n\t\t\t\t\"x\": 911,\n\t\t\t\t\"w\": 118,\n\t\t\t\t\"h\": 205\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179364,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 601,\n\t\t\t\t\"x\": 930,\n\t\t\t\t\"w\": 116,\n\t\t\t\t\"h\": 213\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179398,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 605,\n\t\t\t\t\"x\": 944,\n\t\t\t\t\"w\": 117,\n\t\t\t\t\"h\": 216\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179429,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 604,\n\t\t\t\t\"x\": 957,\n\t\t\t\t\"w\": 118,\n\t\t\t\t\"h\": 222\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179470,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 603,\n\t\t\t\t\"x\": 966,\n\t\t\t\t\"w\": 119,\n\t\t\t\t\"h\": 230\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179511,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 602,\n\t\t\t\t\"x\": 971,\n\t\t\t\t\"w\": 120,\n\t\t\t\t\"h\": 233\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179547,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 608,\n\t\t\t\t\"x\": 976,\n\t\t\t\t\"w\": 124,\n\t\t\t\t\"h\": 232\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179582,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 614,\n\t\t\t\t\"x\": 978,\n\t\t\t\t\"w\": 126,\n\t\t\t\t\"h\": 233\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179623,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 618,\n\t\t\t\t\"x\": 980,\n\t\t\t\t\"w\": 126,\n\t\t\t\t\"h\": 230\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179664,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 624,\n\t\t\t\t\"x\": 984,\n\t\t\t\t\"w\": 125,\n\t\t\t\t\"h\": 234\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179702,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 626,\n\t\t\t\t\"x\": 1001,\n\t\t\t\t\"w\": 124,\n\t\t\t\t\"h\": 237\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179732,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 627,\n\t\t\t\t\"x\": 1013,\n\t\t\t\t\"w\": 123,\n\t\t\t\t\"h\": 243\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179783,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 633,\n\t\t\t\t\"x\": 1030,\n\t\t\t\t\"w\": 123,\n\t\t\t\t\"h\": 256\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179821,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 639,\n\t\t\t\t\"x\": 1042,\n\t\t\t\t\"w\": 136,\n\t\t\t\t\"h\": 261\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179862,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 643,\n\t\t\t\t\"x\": 1046,\n\t\t\t\t\"w\": 147,\n\t\t\t\t\"h\": 258\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179897,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 643,\n\t\t\t\t\"x\": 1046,\n\t\t\t\t\"w\": 154,\n\t\t\t\t\"h\": 262\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179937,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 652,\n\t\t\t\t\"x\": 1049,\n\t\t\t\t\"w\": 157,\n\t\t\t\t\"h\": 263\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830179968,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 658,\n\t\t\t\t\"x\": 1054,\n\t\t\t\t\"w\": 161,\n\t\t\t\t\"h\": 265\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180002,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 661,\n\t\t\t\t\"x\": 1057,\n\t\t\t\t\"w\": 162,\n\t\t\t\t\"h\": 267\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180044,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 659,\n\t\t\t\t\"x\": 1062,\n\t\t\t\t\"w\": 167,\n\t\t\t\t\"h\": 270\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180085,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 664,\n\t\t\t\t\"x\": 1070,\n\t\t\t\t\"w\": 169,\n\t\t\t\t\"h\": 270\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180116,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 669,\n\t\t\t\t\"x\": 1078,\n\t\t\t\t\"w\": 175,\n\t\t\t\t\"h\": 266\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180155,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 671,\n\t\t\t\t\"x\": 1112,\n\t\t\t\t\"w\": 174,\n\t\t\t\t\"h\": 268\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180189,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 671,\n\t\t\t\t\"x\": 1128,\n\t\t\t\t\"w\": 174,\n\t\t\t\t\"h\": 272\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180227,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 673,\n\t\t\t\t\"x\": 1142,\n\t\t\t\t\"w\": 174,\n\t\t\t\t\"h\": 272\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180259,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 693,\n\t\t\t\t\"x\": 1151,\n\t\t\t\t\"w\": 176,\n\t\t\t\t\"h\": 276\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180294,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 702,\n\t\t\t\t\"x\": 1157,\n\t\t\t\t\"w\": 179,\n\t\t\t\t\"h\": 275\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180334,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 702,\n\t\t\t\t\"x\": 1171,\n\t\t\t\t\"w\": 178,\n\t\t\t\t\"h\": 285\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180372,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 703,\n\t\t\t\t\"x\": 1178,\n\t\t\t\t\"w\": 176,\n\t\t\t\t\"h\": 289\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180409,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 689,\n\t\t\t\t\"x\": 1199,\n\t\t\t\t\"w\": 180,\n\t\t\t\t\"h\": 280\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180445,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 704,\n\t\t\t\t\"x\": 1214,\n\t\t\t\t\"w\": 178,\n\t\t\t\t\"h\": 284\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180484,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 709,\n\t\t\t\t\"x\": 1245,\n\t\t\t\t\"w\": 184,\n\t\t\t\t\"h\": 289\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180519,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 717,\n\t\t\t\t\"x\": 1259,\n\t\t\t\t\"w\": 184,\n\t\t\t\t\"h\": 291\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180562,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 719,\n\t\t\t\t\"x\": 1283,\n\t\t\t\t\"w\": 184,\n\t\t\t\t\"h\": 293\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180605,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 706,\n\t\t\t\t\"x\": 1326,\n\t\t\t\t\"w\": 202,\n\t\t\t\t\"h\": 306\n\t\t\t}\n\t\t}, {\n\t\t\t\"frameIndex\": 1571830180652,\n\t\t\t\"base64Data\": \"\",\n\t\t\t\"location\": {\n\t\t\t\t\"y\": 723,\n\t\t\t\t\"x\": 1348,\n\t\t\t\t\"w\": 198,\n\t\t\t\t\"h\": 303\n\t\t\t}\n\t\t}],\n\t\t\"trackId\": 172,\n\t\t\"enterFlag\":1,\n\t\t\"trackFlag\":1,\n\t\t\"enterTime\":34343434\n\t}],\n\t\"sId\": \"129.127\",\n\t\"eId\": \"1\"\n}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
#
原來的
# m=MultipartEncoder(
# fields={
# 'json':{
# "sId": "3434343",
# "eId": "34343",
# "cId": "23",
# "trackList": [{
# "trackId": 343545454,
# "trackFlag":1,
# "enterFlag":1,
# "frameList": [{
# "frameIndex":0,
# "imageIndex":0,
# "base64Data": "",
# "location": {
# "x": 1.66,
# "y": 15.72,
# "w": 2.34,
# "h": 43.12
# }
# },
# 'files': (files_name[0], image1,"image/jpeg")})
data_json={
"sId": "3434343",
"eId": "34343",
"cId": "23",
"trackList": [{
"trackId": "343545454",
"trackFlag":1,
"enterFlag":1,
"frameList": [{
"frameIndex":0,
"imageIndex":0,
"base64Data": "",
"location": {
"x": 1.66,
"y": 15.72,
"w": 2.34,
"h": 43.12
}
}
data_str=json.dumps(data_json)
m=MultipartEncoder(
fields={
'json':data_str,
'files': (files_name[0], image1,"image/jpeg")})
r = requests.post(url, data=m,headers={'Content-Type': m.content_type})
print(r.text)

更多技術資訊可關注:itheimaGZ獲取


相關文章