Skip to main content

aiohttp 库的人性化包装器,用于发出异步 Web 请求

项目描述

服务

aiohttp 库的包装器,用于在 Python 中发出异步 Web 请求。

在 Python 中发出异步 HTTP 请求时,尝试使用客户端会话和上下文管理器来保持抽象提供的速度和灵活性, aiohttp而不牺牲人类友好性。requestsservus

示例用法:

import servus
import aiohttp
import asyncio

async def main():
	# Create a new session
	mySession = aiohttp.ClientSession()
	
	# Use Servus to send a request. 
	# Servus automatically parses and serializes the response, and returns a ready to use object
	response = await servus.get(mySession, "http://httpbin.org")
	
	print(response.response) # (aiohttp.ClientResponse )
	print(response.json) # (dict)

	# Remeber to close the session!
	mySession.close()

asyncio.run(main())

servus还内置了对使用 Discord 机器人的支持。

示例用法:

import discord
from discord.ext import commands
import asyncio
import servus
from servus.discord_utils import createRequestsClient

MY_TOKEN = "<YOUR_TOKEN>"
bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"))


@bot.command()
async  def hello(ctx):
	"""Hello world, with a HTTP request!"""
	r = await servus.get(bot.session,"https://httpbin.org")
	data = r.json
	await ctx.send(f"World! {data}")

# Add the createRequestClient coroutine to `bot` async loop
bot.loop.create_task(createRequestsClient(bot))

# Run the bot
bot.run(MY_TOKEN)

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

servus-1.0.0.tar.gz (15.9 kB 查看哈希

已上传 source

内置分布

servus-1.0.0-py3-none-any.whl (16.4 kB 查看哈希

已上传 py3