代码拉取完成,页面将自动刷新
import os
from wordpress_xmlrpc import Client
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media
#pip install python-wordpress-xmlrpc
def post_image(domain,loginuser,loginpass,picpath,picfilename):
'''
wordpress 自动上传图片
:param domain: 域名
:param loginuser: 用户名
:param loginpass: 密码
:param picpath: 图片路径
:param picfilename: 图片名字
:return: 返回图片url
'''
wp = Client(f'http://{domain}/xmlrpc.php', loginuser, loginpass)
filename = f'{picpath}' # 上传的图片文件路径
# prepare metadata
data = {
'name': f'{picfilename}.jpg',
'type': 'image/jpeg', # mimetype
}
# read the binary file and let the XMLRPC library encode it into base64
with open(filename, 'rb') as img:
data['bits'] = xmlrpc_client.Binary(img.read())
response = wp.call(media.UploadFile(data))
print(response)
return response.get('link')
if __name__ == '__main__':
# domain = 'www.wpauto.io'
# loginuser = 'admin'
# loginpass = 'admin'
# filepath = 'default.jpg'
# filename = '这是图片的标题'
# imgurl = post_image(domain=domain,loginuser=loginuser,loginpass=loginpass,picpath=filepath,picfilename=filename)
# print(imgurl)
path = os.path.split(os.path.realpath(__file__))[0]
print(path)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。