Press ESC to close

wordpress 使用python上传图片2

代码:

import requests
from urllib.parse import urljoin
import os
import json

def wp_upload_media():
    url_base = 'https://www.xatest.net/'
    url = urljoin(url_base, 'wp-json/wp/v2/media/')

    user = "username"
    password = "yHAW COZa hxC3 Rr67 1IRP jagE"

    filename = 'pexels-martin-schuitemaker-5858100.jpg'
    file_path = r'D:\处理之后\' + filename
    print(file_path)
    f = open(file_path, 'rb')
    image_data = f.read()
    f.close()

    headers = {
        'Content-Type': 'image/png',
        'Content-Disposition': 'attachment; filename=' + filename,
    }

    res = requests.post(
        url,
        data=image_data,
        headers=headers,
        auth=(user, password),
        )
    res_dict = res.json()
    print(json.dumps(res_dict, indent=4))
    unique_id = res_dict['id']  
    return

if __name__ == '__main__':
    wp_upload_media()

发表回复