遍历目录到 post 图片到通过 API
Iterate though directory to post pictures to via API
我想 运行 对目录中的每个文件进行此 Web 调用,将路径 "C:\Users\user\Pictures\279259.jpg\"
可变化到 Web 调用中。我想我可以使用 os.path.join
但不知道如何格式化任何帮助将不胜感激
import http.client
conn = http.client.HTTPSConnection("some.website.com")
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data\"; filename=\"C:\Users\user\Pictures\279259.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'cache-control': "no-cache",
}
conn.request("POST", "/api/users/pictures", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
如果有人有更优雅的解决方案,我会很乐意看到。
import os
import http.client
Files={}
path = "path"
for folder, subfolder, files in os.walk(os.getcwd()):
for file in files:
Files[file] = os.path.dirname(file)
for file in Files:
print ((path) + (file))
conn = http.client.HTTPSConnection("website")
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data\"; filename=\""+((path)+(file))+"\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
我想 运行 对目录中的每个文件进行此 Web 调用,将路径 "C:\Users\user\Pictures\279259.jpg\"
可变化到 Web 调用中。我想我可以使用 os.path.join
但不知道如何格式化任何帮助将不胜感激
import http.client
conn = http.client.HTTPSConnection("some.website.com")
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data\"; filename=\"C:\Users\user\Pictures\279259.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'cache-control': "no-cache",
}
conn.request("POST", "/api/users/pictures", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
如果有人有更优雅的解决方案,我会很乐意看到。
import os
import http.client
Files={}
path = "path"
for folder, subfolder, files in os.walk(os.getcwd()):
for file in files:
Files[file] = os.path.dirname(file)
for file in Files:
print ((path) + (file))
conn = http.client.HTTPSConnection("website")
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data\"; filename=\""+((path)+(file))+"\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"