如何发送帖子请求并获取文件? Python Django
问题描述:
一个站点iPXE生成imagea文件,我需要通过发送请求来下载。如何发送帖子请求并获取文件? Python Django
我想在第3个网站(rom-o-matic.eu)上发布帖子,并从网站获取文件。这可能吗?
我的例子是这样的:
def requestPOST(request):
values = {'wizardtype': 'standard',
'outputformatstd': 'bin/ipxe.usb',
'embed': '#!ipxe dhcp route}',
'gitrevision': 'master'}
r = requests.post("https://rom-o-matic.eu/", verify=False, data={values})
return()
什么要这样的回报?
谢谢。
答
import requests
import shutil
def downloadPOST(outpath):
values = {
'wizardtype': 'standard',
'outputformatstd': 'bin/ipxe.usb',
'embed': '#!ipxe dhcp route}',
'gitrevision': 'master',
}
r = requests.get("https://rom-o-matic.eu/", data={values}, verify=False, stream=True)
if r.status_code != 200:
raise ValueError('Status code != 200')
with open(outpath, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
我建议你阅读官方 “请求” 文档。你可以在这里找到很多例子:http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests – Antoine