简体   繁体   English

如何使用Python(POST请求)将图像上传到MediaWiki?

[英]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.

相关问题 如何使用Python将文件上传到MediaWiki? - How to upload files to MediaWiki using Python? 如何使用带有POST请求的python在Google云端硬盘上传文件? - How to upload a file on Google Drive using python with POST request? 如何使用请求发布 python 将照片上传到 Instagram? - How to upload a photo to Instagram using request post python? python 使用 PIL Image 发布请求 - python post request using PIL Image 如何使用请求 POST 方法上传 PDF 文件,并且请求必须使用用于 xeroAPI 的 python 格式化为多部分 MIME? - How to upload PDF file using request POST method and requests must be formatted as multipart MIME using python for xeroAPI? Python 带有图像的 POST 请求 - Python POST Request with an Image Python 发布图片请求 - Python Post request for image 如何使用表单数据在 Robot Framework/Python 中创建发布请求以上传文件和其他键值 - How to create a post request in Robot Framework/ Python using form-data to upload files and other key values 如何通过发送带有应用程序/八位字节流内容类型的“ POST”请求,使用python将视频上传到Microsoft服务 - How to upload a video to Microsoft service using python by sending a “POST” request with application/octet-stream content type 如何使用python请求使用图像和文本有效负载调用REST API发布请求? - How to call REST API post request with image and text payload using python request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM