Python和pushbullet api:发送文件

Python和pushbullet api:发送文件,python,python-requests,pushbullet,Python,Python Requests,Pushbullet,我正试图用Pushbullet发送一个文件。 这是我的职责: def push_file(AccessToken, file_name): f = open(file_name, 'rb') file_type = mimetypes.guess_type(file_name)[0] print("Uploading {0}...".format(file_name)) try: data = { 'file_name':

我正试图用Pushbullet发送一个文件。 这是我的职责:

def push_file(AccessToken, file_name):
    f = open(file_name, 'rb')
    file_type = mimetypes.guess_type(file_name)[0]

    print("Uploading {0}...".format(file_name))
    try:
        data = {
            'file_name': file_name, 
            'file_type' : file_type
        }

        resp = requests.post(UPLOAD_REQUEST_URL, data=data, auth=(AccessToken, '')).json()
        if resp.get('error') != None:
            print("Error: {0}".format(resp.get('error')['message']))
            return

        file_url = resp.get('file_url')
        print(file_url)
        resp = requests.post(resp.get('upload_url'), data=resp.get('data'), auth=(AccessToken, ''), files={'file': f})

        data = { 
            'type' : 'file', 
            'file_name' : file_name, 
            'file_type' : file_type, 
            'file_url' : file_url, 
            'body' : ''
        }
        resp = requests.post(PUSH_URL, data=data, auth=(AccessToken, '')).json()


    except requests.exceptions.ConnectionError:
        traceback.print_exc()
    f.close()
但我一直得到:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='s3.amazonaws.com', port=443): Max retries exceeded with url: /pushbullet-uploads (Caused by <class 'ConnectionResetError'>: [Errno 104] Connection reset by peer)
requests.exceptions.ConnectionError:HTTPSConnectionPool(host='s3.amazonaws.com',port=443):url:/pushbullet上载超过最大重试次数(由对等方重置:[Errno 104]连接引起)

如果我使用另一个AccessToken,即使这是我第一次发布到该url,我仍然会收到此错误。

根据Pusbill API

所有POST请求都应该通过HTTPS,并使用JSON正文和 内容类型标题设置为“application/json”

尝试更改您的
请求。按如下方式发布
呼叫:

resp = requests.post(UPLOAD_REQUEST_URL, json=data, auth=(AccessToken, '')).json()

使用
json=data
而不是
data=data
。请求将自动将
内容类型
设置为
应用程序/json

不幸的是,上传过程不是很好,希望很快会得到改进。这是一个不遵守JSON规则的请求。有一个curl示例显示了这一点(),但是理解curl语法基本上是不可能的

下面是我刚刚输入的一个示例,它似乎很有效:

import requests
import json

ACCESS_TOKEN = '<your access token here>'
resp = requests.post('https://api.pushbullet.com/v2/upload-request', data=json.dumps({'file_name': 'image.jpg'}), headers={'Authorization': 'Bearer ' + ACCESS_TOKEN, 'Content-Type': 'application/json'})
if resp.status_code != 200:
    raise Exception('failed to request upload')
r = resp.json()
resp = requests.post(r['upload_url'], data=r['data'], files={'file': open('image.jpg', 'rb')})
if resp.status_code != 204:
    raise Exception('failed to upload file')
print r['file_name'], r['file_type'], r['file_url']
导入请求
导入json
访问令牌=“”
resp=requests.post('https://api.pushbullet.com/v2/upload-request,data=json.dumps({'file_name':'image.jpg'}),headers={'Authorization':'Bearer'+ACCESS_-TOKEN,'Content-Type':'application/json'})
如果响应状态\ U代码!=200:
引发异常('请求上载失败')
r=resp.json()
resp=requests.post(r['upload_url'],data=r['data'],files={'file':open('image.jpg','rb'))
如果响应状态\ U代码!=204:
引发异常('未能上载文件')
打印r['文件名'],r['文件类型'],r['文件url']

我不能使用json=data,我认为我的计算机上安装的请求版本太旧了。我还尝试手动设置headers={'Content-type':'application/json'},但我一直得到相同的结果…是否尝试更新请求
pip安装请求==2.5.3
谢谢您!其他人注意:您必须在Chris的代码之后添加最终推送行,才能真正进行推送。对于那些存在类似“”的问题的用户,请按照此处的指南操作:即手动安装所需的实用程序,如cffi、enum34、ipaddress、idna、pycparser、cryptography。