【问题标题】:Python requests' POST file fails when trying to upload a WordPress Theme to Host尝试将 WordPress 主题上传到主机时,Python 请求的 POST 文件失败
【发布时间】:2017-06-15 01:37:52
【问题描述】:

我正在尝试编写一个 python 脚本来帮助我远程安装主题。不幸的是,上传部分效果不佳,尝试使用请求的 POST 助手来完成。

成功上传的 HTTP 标头如下所示:

http://127.0.0.1/wordpress/wp-admin/update.php?action=upload-theme

POST /wordpress/wp-admin/update.php?action=upload-theme HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------2455316848522
Content-Length: 2580849
Referer: http://127.0.0.1/wordpress/wp-admin/theme-install.php
Cookie: wordpress_5bd7a9c61cda6e66fc921a05bc80ee93=admin%7C1497659497%7C4a1VklpOs93uqpjylWqckQs80PccH1QMbZqn15lovQu%7Cee7366eea9b5bc9a9d492a664a04cb0916b97b0d211e892875cec86cf43e2f9d; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_5bd7a9c61cda6e66fc921a05bc80ee93=admin%7C1497659497%7C4a1VklpOs93uqpjylWqckQs80PccH1QMbZqn15lovQu%7C9949f19ef5d900daf1b859c0bb4e2129cf86d6a970718a1b63e3b9e56dc5e710; wp-settings-1=libraryContent%3Dbrowse; wp-settings-time-1=1497486698
Connection: keep-alive
Upgrade-Insecure-Requests: 1
-----------------------------2455316848522: undefined
Content-Disposition: form-data; name="_wpnonce"
b1467671e0
-----------------------------2455316848522
Content-Disposition: form-data; name="_wp_http_referer"

/wordpress/wp-admin/theme-install.php
-----------------------------2455316848522
Content-Disposition: form-data; name="themezip"; filename="oedipus_theme.zip"
Content-Type: application/octet-stream

PK

HTTP/1.1 200 OK
Date: Thu, 15 Jun 2017 01:33:25 GMT
Server: Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1
X-Powered-By: PHP/7.1.1
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
X-Frame-Options: SAMEORIGIN
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
----------------------------------------------------------

为 WP 创建一个简单的会话,以便稍后用于上传:

global wp_session

def wpCreateSession(uname, upassword, site_link):
    """
    :param uname: Username for the login.
    :param upaswword: Password for the login. 
    :param site_link: Site to login on.
    :return: Returns a sessions for the said website.
    """
    global wp_session
    wp_session = requests.session()
    wp_session.post(site_link, data={'log' : uname, 'pwd' : upassword})

要将上述文件上传到 WP,使用 wp_session 全局:

def wpUploadTheme(file_name):

    global wp_session

    try:
        with open(file_name, 'rb') as up_file:
            r = wp_session.post('http://127.0.0.1/wordpress/wp-admin/update.php', files = {file_name: up_file})
            print "Got after try."
    finally:
        up_file.close()

最后一点是它不起作用的地方,上传不成功,我返回到 WordPress 的基本 404。

我也试过 requests_toolbelt MultiPart_Encoder 无济于事。

【问题讨论】:

    标签: python wordpress post http-headers http-post


    【解决方案1】:

    问题:'requests' POST 文件在尝试上传时失败

    检查您的files dict,您的dict 无效

    files = {file_name: up_file}
    

    也许你需要一个完整的files dict,例如:

    files = {'themezip': ('oedipus_theme.zip', 
                           open('oedipus_theme.zip', 'rb'), 
                          'application/octet-stream', {'Expires': '0'})}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-09
      • 2020-06-20
      • 2019-11-29
      • 2012-03-17
      • 2021-12-19
      • 2016-05-20
      • 1970-01-01
      • 2016-02-13
      相关资源
      最近更新 更多