Python, Pydio, Rest and module requests to send a file

260 views Asked by At

Totally newbie on python, I had to try to upload a gzip file using requests module and python3.5 launched from a freeBSD bash.

Here is my upload.py after gathering the Internet information I managed to get:

#!/usr/bin/env python3.5
import requests, os.path, sys, glob, time, re, datetime
urlPydio ='https://remote.pydio.server.fr'
certPydio = 'remote.pydio.server.ssl.crt'
depotDistant = 'ws'
urlComplet = urlPydio + "/api/" + depotDistant + "/"
loginPydio = 'user.name'`
passwordPydio = 'PASSWORD'
repLocal = os.path.abspath('/var/squid/logs/access_log_gz/')
yesterday = datetime.date.today() - datetime.timedelta(days=1)
listFichiers = os.listdir(repLocal)

for fichier in listFichiers:`
    if re.search ('^\w{5}_\w{4}_\w+_'+ str(yesterday) + '.gz$', fichier):
        nomFichierComplet = repLocal +'/'+ fichier
        headers = {'x-File-Name': fichier}
        urlAction = urlComplet + "upload/input_stream" + "/remote_folder/remote_folder/"
        print(nomFichierComplet, urlAction)
        files = {fichier: open(nomFichierComplet, 'rb')}
    try:
        rest = requests.put(urlAction, files=files, auth=(loginPydio,passwordPydio), headers=headers, verify=False)
        codeRetour = rest.status_code
        if codeRetour == 200:
            print('file sent succcessfully', codeRetour, rest.headers)
        else:
            print('error sending file ', codeRetour, rest.text)
    except requests.exceptions.RequestException as e:
        print(e)

As a result, I managed to get the log.gz file on remote pydio server but there is some headers stuff added in the file, making it impossible to unzip. Open with a notepad, the 3 lines to remove are :

--223ef42df08f4792ba6ef6e71cdf749c

Content-Disposition: form-data; name="log.gz"; filename="log.gz"

I tried some request.post stuff, unsuccessful, I tried to change headers values to None, no way to send the file. I also tried to rest.prepare() to del headers['Content-Disposition'] unsuccessfully too

rest.headers returns :

{'Content-Type': 'text/html; charset=UTF-8', 'Date': 'Thu, 05 Jan 2017 10:10:16 GMT', 'Transfer-Encoding': 'chunked', 'Set-Cookie': 'AjaXplorer=i6m1ud1cgocokaehc58gelc8m2; path=/; HttpOnly', 'Vary': 'Accept-Encoding', 'Connection': 'keep-alive', 'Server': 'nginx', 'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Content-Encoding': 'gzip', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Pragma': 'no-cache'}

Now, I'm here, asking you some help or some papers/posts to read. I just want to upload a log.gz file without any change to the file itself so it can be downloaded and unzipped.

0

There are 0 answers