requests:
import requests
# 要上传的文件路径
file_path = 'requirements.txt'
# 目标上传URL
url = 'http://httpbin.org/post'
# 使用requests上传文件
files = {'file': open(file_path, 'rb')}
response = requests.post(url, files=files)
# 打印响应内容
print(response.text)
aiohttp:
import asyncio
import aiohttp
async def upload_file(file_path):
async with aiohttp.ClientSession() as session:
with open(file_path, 'rb') as file:
data = aiohttp.FormData()
data.add_field('file', file, filename='custom_filename')
async with session.post('http://example.com/upload', data=data) as response:
print(await response.text())
# 使用方法:
asyncio.run(upload_file('path/to/your/file.ext'))