Python、Pydio、Rest和模块请求发送fi

2024-09-27 17:45:58 发布

您现在位置:Python中文网/ 问答频道 /正文

对于python完全是新手,我不得不尝试使用requests模块和从freeBSD bash启动的python3.5上传gzip文件。在

这是我的上传.py在收集了互联网信息后,我设法得到:

#!/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)

结果,我设法得到了日志.gz文件位于远程pydio服务器上,但文件中添加了一些头文件,因此无法解压缩。用记事本打开,要删除的3行是:

--223ef42df08f4792ba6ef6e71cdf749c

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

我试了一些请求.post资料,不成功,我试图将标题值改为None,无法发送文件。 我也试着休息。准备内容删除不成功

rest.headers返回:

{'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'}

现在,我在这里,请你帮忙或是一些论文/帖子来阅读。 我只想上传一个日志.gz文件本身没有任何更改,因此可以下载和解压缩。在


Tags: 文件pathlogrestdatetimeremoteosrequests

热门问题