Python 如何使用get请求的响应通过aiohttp上传文件?
首先,我正在为WordPressRESTAPI编写一个异步包装器。我在Bluehost上有一个Wordpress网站。我正在使用媒体(图像)上传的端点。我已经成功地上传了一张图片,但是我想做两个改变。第二个变化是我真正想要的,但出于好奇,我也想知道如何实现变化1。我将首先提供代码,然后提供一些详细信息 工作代码Python 如何使用get请求的响应通过aiohttp上传文件?,python,wordpress,aiohttp,python-aiofiles,Python,Wordpress,Aiohttp,Python Aiofiles,首先,我正在为WordPressRESTAPI编写一个异步包装器。我在Bluehost上有一个Wordpress网站。我正在使用媒体(图像)上传的端点。我已经成功地上传了一张图片,但是我想做两个改变。第二个变化是我真正想要的,但出于好奇,我也想知道如何实现变化1。我将首先提供代码,然后提供一些详细信息 工作代码 async def upload_local_pic2(self, local_url, date, title): url = f'{self.base_url}/wp-jso
async def upload_local_pic2(self, local_url, date, title):
url = f'{self.base_url}/wp-json/wp/v2/media'
with aiohttp.MultipartWriter() as mpwriter:
json = {'title': title, 'status':'publish'}
mpwriter.append_json(json)
with open(local_url, 'rb') as f:
print(f)
payload = mpwriter.append(f)
async with self.session.post(url, data=payload) as response:
x = await response.read()
print(x)
更改1
第一个变化是使用aiofiles.open()上传,而不是像我预期的那样只使用open()来处理大量文件。以下代码不起作用
async def upload_local_pic(self, local_url, date, title):
url = f'{self.base_url}/wp-json/wp/v2/media'
with aiohttp.MultipartWriter() as mpwriter:
json = {'title': title, 'status':'publish'}
mpwriter.append_json(json)
async with aiofiles.open(local_url, 'rb') as f:
print(f)
payload = mpwriter.append(f)
async with self.session.post(url, data=payload) as response:
x = await response.read()
print(x)
async def upload_pic(self, image_url, date, title):
url = f'{self.base_url}/wp-json/wp/v2/media'
with aiohttp.MultipartWriter() as mpwriter:
json = {'title':title, 'status':'publish'}
mpwriter.append_json(json)
async with self.session.get(image_url) as image_response:
image_content = image_response.content
print(image_content)
payload = mpwriter.append(image_content)
async with self.session.post(url, data = payload) as response:
x = await response.read()
print(x)
更改2
我的另一个变化是,我想有另一个功能,可以直接上传文件到WordPress服务器,而无需本地下载。因此,我不想获取本地图片,而是想在网上传递图片的url。以下代码也不起作用
async def upload_local_pic(self, local_url, date, title):
url = f'{self.base_url}/wp-json/wp/v2/media'
with aiohttp.MultipartWriter() as mpwriter:
json = {'title': title, 'status':'publish'}
mpwriter.append_json(json)
async with aiofiles.open(local_url, 'rb') as f:
print(f)
payload = mpwriter.append(f)
async with self.session.post(url, data=payload) as response:
x = await response.read()
print(x)
async def upload_pic(self, image_url, date, title):
url = f'{self.base_url}/wp-json/wp/v2/media'
with aiohttp.MultipartWriter() as mpwriter:
json = {'title':title, 'status':'publish'}
mpwriter.append_json(json)
async with self.session.get(image_url) as image_response:
image_content = image_response.content
print(image_content)
payload = mpwriter.append(image_content)
async with self.session.post(url, data = payload) as response:
x = await response.read()
print(x)
详细信息/调试
我想弄明白为什么每一个都不起作用。我认为关键在于调用print(图像内容)
和print(f)
显示我输入到mpwriter.append的确切内容
在我使用标准Pythonopen()
函数的示例中,我显然是在传递
在带有aiofile的change1示例中,我传入的是我传入change2示例的url。好的,所以我计算出了这两个更改
对于尝试使用aiofiles
读取文件时的第一个更改,我需要读取整个文件,而不是传递文件处理程序。另外,我需要手动设置内容配置
async def upload_local_pic(self, local_url, date, title):
url = f'{self.base_url}/wp-json/wp/v2/media'
with aiohttp.MultipartWriter() as mpwriter:
json = {'status':'publish'}
mpwriter.append_json(json)
async with aiofiles.open(local_url, mode='rb') as f:
contents = await f.read()
payload = mpwriter.append(contents)
payload.set_content_disposition('attachment', filename= title+'.jpg')
async with self.session.post(url, data=payload) as response:
x = await response.read()
print(x)
async def upload_pic(self, image_url, date, title):
url = f'{self.base_url}/wp-json/wp/v2/media'
with aiohttp.MultipartWriter() as mpwriter:
json = {'status':'publish'}
mpwriter.append_json(json)
async with self.session.get(image_url) as image_response:
image_content = await image_response.read()
payload = mpwriter.append(image_content)
payload.set_content_disposition('attachment', filename=title+'.jpg')
async with self.session.post(url, data = payload) as response:
x = await response.read()
print(x)
对于第二个变化,这是一个类似的概念,只是直接从URL上传一个文件。我需要先读取整个内容,而不是传入将读取内容的处理程序。我还需要手动设置内容配置
async def upload_local_pic(self, local_url, date, title):
url = f'{self.base_url}/wp-json/wp/v2/media'
with aiohttp.MultipartWriter() as mpwriter:
json = {'status':'publish'}
mpwriter.append_json(json)
async with aiofiles.open(local_url, mode='rb') as f:
contents = await f.read()
payload = mpwriter.append(contents)
payload.set_content_disposition('attachment', filename= title+'.jpg')
async with self.session.post(url, data=payload) as response:
x = await response.read()
print(x)
async def upload_pic(self, image_url, date, title):
url = f'{self.base_url}/wp-json/wp/v2/media'
with aiohttp.MultipartWriter() as mpwriter:
json = {'status':'publish'}
mpwriter.append_json(json)
async with self.session.get(image_url) as image_response:
image_content = await image_response.read()
payload = mpwriter.append(image_content)
payload.set_content_disposition('attachment', filename=title+'.jpg')
async with self.session.post(url, data = payload) as response:
x = await response.read()
print(x)