【发布时间】:2021-01-09 23:57:56
【问题描述】:
我正在尝试使用 python 将文件上传到 wordpress 站点。到目前为止,我似乎无法在没有扩展的情况下将 API 与会话 cookie 一起使用。所以在这一点上,我正在尝试跟随以下帖子
Login Wordpress with requests - 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 中提取数据字段,直接发布到 async-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。我会假设我实际上并没有进行身份验证。
【问题讨论】:
标签: python wordpress python-requests upload