我试图上传文件到一个WordPress网站使用Python.到目前为止,它看起来像我不能使用API与会话cookie没有扩展.所以在这一点上,我试图按照沿着以下职位
使用请求登录WordPress- Python3
Uploading of image to WordPress through Python's requests
这就是我目前所知道的。
#!/usr/bin/python3
import sys, requests
f = 'test.txt'
user='username'
password='password'
url1='https://example.com/wp-login.php'
url2='https://example.com/wp-admin/media-new.php'
headerauth= {'Cookie':'wordpress_test_cookie=WP Cookie check'}
dataauth = {'log':user, 'pwd':password, 'wp-submit':'Log In'}
dataupload = {'post_id': '0', '_wp_http_referer': '/wp-admin/media-new.php', 'action': 'upload_attachement', 'html-upload': 'Upload'}
image = {'async-upload':('test.txt', open(f, "rb"))}
session1=requests.session()
r1 = session1.post(url1, headers=headerauth, data=dataauth)
print(r1)
r2 = session1.get(url2)
print(r2)
r3 = session1.post(url2, data=dataupload, files=image)
print(r3)
字符串
当运行这个程序时,我得到了以下响应,显然最后一个是有趣的。
./upload.py
<Response [200]>
<Response [200]>
<Response [403]>
型
我也试过在手动上传文件后从Chrome中提取数据字段,直接发布到UBC-upload.php,结果类似。
更新:我收到的回复页面有以下标题。
<title>Something went wrong.</title>
...
<body id="error-page">
<div class="wp-die-message">The link you followed has expired.</div>
</body>
型
我还添加了nonce值后,挖掘了周围的网页源代码。
<input type="hidden" id="_wpnonce" name="_wpnonce" value="74bdb561c5">
型
这是我加的。
r2 = session1.get(url2)
test = re.search('value="[0-9a-z]{10}"', r2.text)
nonce = re.search('[0-9a-z]{10}', test.group(0))
nonce = nonce.group(0)
dataupload = {'post_id':'0', '_wp_http_referer':'/wp-admin/media-new.php', '_wpnonce':nonce, 'action':'upload_attachement', 'html-upload':'Upload'}
型
仍然没有运气。我还注意到,与我的基于浏览器的会话相比,缺少cookie。我将假设我实际上没有进行身份验证。
3条答案
按热度按时间kzipqqlq1#
您可能需要在请求中添加更多标头。
标题可以在
Network > Headers > Request Headers of the Developer Tools.
中找到(按F12切换它。)wlsrxk512#
使用了错误的随机数。要提取正确的随机数,请使用以下命令并发布到Media-upload.php。关键是从media-new.php页面提取form _wpnonce。
如果你不从表单参数中提取,那么你最终可能会得到一打其他随机数中的一个。
字符串
完整的代码是
型
yx2lnoni3#
在WordPress API v2上通过Python 3脚本测试了多种文件类型后,我们发布了一个要点。它上传到WordPress默认上传目录,当然您可以在其中修改其设置:
字符串
该脚本也以安全的方式处理令牌。
希望对你也有用:
型
在另一个文件中:
型
使用这个脚本,你可以有多个WordPress网站的字典,并更新他们都与一个单一的功能。如果你想有一个所有在一个模块,随时合并的脚本在一起。
The Gist on GitHub