跳转到主要内容

Python语言访问Akismet垃圾邮件过滤服务的接口。

项目描述

CI status image

Python语言访问Akismet垃圾邮件过滤服务的接口。

此库提供两个API客户端

  • akismet.SyncClient是一个Akismet API客户端,它对Akismet网络服务执行同步(阻塞)HTTP请求。

  • akismet.AsyncClient是一个Akismet API客户端,它对Akismet网络服务执行异步(async/await/非阻塞)HTTP请求。

除了一个是同步的,另一个是异步的之外,这两个客户端公开相同的API,并实现了Akismet网络API的所有方法。

要使用此库,您需要获取一个Akismet API密钥并为使用Akismet网络服务注册一个网站;您可以在 <https://akismet.com> 完成此操作。一旦您拥有一个密钥及其相应的注册网站URL,请将其放置在环境变量 PYTHON_AKISMET_API_KEYPYTHON_AKISMET_BLOG_URL 中,它们将自动检测并使用。

然后您可以构建客户端实例并调用其方法。对于创建一个长期运行的API客户端实例,建议您使用 validated_client() 构造函数方法,该方法将自动使用Akismet网络服务验证您的API密钥。例如,为了检查提交的论坛帖子是否为垃圾邮件

import akismet

akismet_client = akismet.SyncClient.validated_client()

if akismet_client.comment_check(
    user_ip=submitter_ip,
    comment_content=submitted_content,
    comment_type="forum-post",
    comment_author=submitter_name
):
    # This piece of content was classified as spam; handle it appropriately.

或者使用异步客户端

import akismet

akismet_client = await akismet.AsyncClient.validated_client()

if await akismet_client.comment_check(
    user_ip=submitter_ip,
    comment_content=submitted_content,
    comment_type="forum-post",
    comment_author=submitter_name
):
    # This piece of content was classified as spam; handle it appropriately.

您还可以将任意客户端类用作上下文管理器。这不需要 validated_client() 构造函数,因为您的API密钥在进入 with 块时会进行验证。

import akismet

with akismet.SyncClient() as akismet_client:
    if akismet_client.comment_check(
        user_ip=submitter_ip,
        comment_content=submitted_content,
        comment_type="forum-post",
        comment_author=submitter_name
    ):
        # This piece of content was classified as spam; handle it appropriately.

或者使用异步客户端

import akismet

async with akismet.AsyncClient() as akismet_client:
    if await akismet_client.comment_check(
        user_ip=submitter_ip,
        comment_content=submitted_content,
        comment_type="forum-post",
        comment_author=submitter_name
    ):
        # This piece of content was classified as spam; handle it appropriately.

有关详细信息,请参阅 文档

此库的原始版本由Michael Foord编写。

项目详情


下载文件

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

源分布

akismet-24.5.1.tar.gz (53.7 kB 查看哈希值)

上传时间

构建分布

akismet-24.5.1-py3-none-any.whl (26.3 kB 查看哈希值)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面