wordpress 使用python上传图片

代码:

import base64, requests, json

def header(user, password):
    credentials = user + ':' + password
    token = base64.b64encode(credentials.encode())
    header_json = {'Authorization': 'Basic ' + token.decode('utf-8')}
    return header_json

def upload_image_to_wordpress(file_path, url, header_json):
    media = {'file': open(file_path,"rb"),'caption': 'My great demo picture'}
    responce = requests.post(url + "wp-json/wp/v2/media", headers = header_json, files = media)
    print(responce.text)

hed = header("username","application password") #username, application password
upload_image_to_wordpress('D:\pexels-alesia-kozik-6629126.jpg', 'https://www.xatest.net/',hed)

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注