Python请求'尝试将WordPress主题上载到主机时POST文件失败

时间:2017-06-15 01:37:53

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

我正在尝试编写一个可以帮助我远程安装主题的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_session global:

将所述文件上传到WP
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。

我也尝试过request_toolbelt MultiPart_Encoder无济于事。

1 个答案:

答案 0 :(得分:0)

  

问题:尝试上传时,“请求”POST文件失败

检查files dictdict无效

files = {file_name: up_file}

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

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