1

I'm trying to write a python script that would help me install a theme remotely. Unfortunately, the upload part doesn't play nice, trying to do it with requests' POST helpers.

The HTTP headers of a successful upload look like this:

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
----------------------------------------------------------

To create a simple session for WP, in order to use later for uploads:

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})

To upload the said file to WP, using the wp_session global:

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()

And this last bit is where it doesn't work, the upload is not successful and I get returned to WordPress' basic 404.

I have also tried requests_toolbelt MultiPart_Encoder to no avail.

Dan Von
  • 83
  • 1
  • 7

1 Answers1

0

Question: 'requests' POST file fails when trying to upload

Check your files dict, your dict is invalid

files = {file_name: up_file}

Maybe you need a full blown files dict, for instance:

files = {'themezip': ('oedipus_theme.zip', 
                       open('oedipus_theme.zip', 'rb'), 
                      'application/octet-stream', {'Expires': '0'})}
stovfl
  • 14,998
  • 7
  • 24
  • 51