[英]How to upload an image to MediaWiki using Python (POST request)?
I'm trying to upload an image file (png or jpg) to MediaWiki using python. 我正在尝试使用python将图像文件(png或jpg)上传到MediaWiki。 I need to make a POST http request.
我需要发出POST http请求。
Here is my code 这是我的代码
mypath = "/Volumes/Home/kate/Downloads/west_wiki/images"
for i in listdir(mypath):
if isfile(join(mypath,i)):
with open(join(mypath,i), 'rb') as content_file:
content = content_file.read()
#get edit token2
params3 = '?format=json&action=query&meta=tokens&continue='
r3 = requests.get(baseurl+'api.php'+params3, cookies=r2.cookies)
edit_token = r3.json()['query']['tokens']['csrftoken']
edit_cookie = r2.cookies.copy()
edit_cookie.update(r3.cookies)
headers = {'content-type': 'image/x-png'}
payload = {'action': 'upload', 'filename':i, 'file': content, 'token': edit_token}
r4 = requests.post(baseurl+'api.php',headers=headers,data=payload, cookies=edit_cookie)
What am I doing wrong? 我究竟做错了什么? Maybe the request is incorrect?
也许请求不正确?
Please use an appropriate Python client . 请使用适当的Python客户端 。
The most commonly used one is upload.py . 最常用的一个是upload.py 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.