访问niddel magnet api v2的python sdk

magnetsdk2的Python项目详细描述


PyPI versionBuild statusDependency Status

Niddel Magnet v2 API Python开发包

一个简单的客户端,允许对Niddel Magnet v2 REST API进行惯用访问。使用美妙的 requests要执行 请求。

发布历史: https://github.com/Niddel/magnet-api2-sdk-python/releases

配置凭据

有两种方法可以让Connection对象知道 要使用的API密钥。最简单的方法是将一个显式传递给 构造器:

frommagnetsdk2importConnectionconn=Connection(api_key="my secret API key")

如果未提供显式api密钥,则Connection构造函数 将在MAGNETSDK_API_KEY环境中首先查找一个 变量并在的default配置文件中失败 配置文件。

您可以使用不同的 通过在 当前用户的主目录。它是一个基本的python配置文件 如下所示:

[default]
api_key=my secret api key

[profile2]
api_key=another secret api key

因此在本例中,您可以创建一个连接来使用 如下:

frommagnetsdk2importConnectionconn_default=Connection()# uses default profileconn_profile2=Connection(profile='profile2')# use profile2 explicitly

使用sdk

创建一个Connection对象并使用它 执行查询。这个小例子向您展示了如何打印出 配置的api密钥可以访问的组织。

importjsonfrommagnetsdk2importConnectionconn=Connection()fororginconn.iter_organizations():print(json.dumps(org,indent=4))

仅下载新警报

使用sdk的一个常见场景是通过 与第三方SIEM或 售票系统。为了实现这一点, 在json文件上保存状态的持久迭代器在 软件开发工具包:

frommagnetsdk2importConnectionfrommagnetsdk2.iteratorimportFilePersistentAlertIteratorconn=Connection()# replace xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx with a valid organization IDalert_iterator=FilePersistentAlertIterator('persistence.json',conn,'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')foralertinalert_iterator:try:# try to process alert in some wayprint(alert)except:alert_iterator.load()# on failure, reload iterator so last alert doesn't count as processedelse:alert_iterator.save()# on success, save iterator so last alert counts as processed

如果您多次运行同一代码,它应该只输出 如果文件persistence.json是 未被篡改,可供阅读和书写。

使用save方法保存迭代器的当前状态。如果 您试图处理警报但失败,您无法保存 迭代器并使用 load方法。

虽然提供的实现将数据保存到json文件中,但是 通过创建 magnetsdk2.iterator.AbstractPersistentAlertIterator实现 抽象的_save_load方法。

命令行实用程序

从版本1.2.0开始,包安装niddel 命令行实用程序,可用于执行大多数相同的 sdk上提供的功能。首先安装软件包:

$ pip install magnetsdk2

然后,您可以看到可以提供一个--profile选项来选择 来自~/.magnetsdk/config的可选api密钥,如前所述 以前:

$ niddel -h
usage: niddel [-h][-p PROFILE][-i][-v][-o OUTFILE]{me,organizations,alerts,logs} ...

Command-line utility to interact with the Niddel Magnet v2 API (v1.4.1)

positional arguments:
  {me,organizations,alerts,logs}
    me                  display API key owner information
    organizations       list basic organization information
    alerts              list an organization's alerts
    logs                upload, download or list log files

optional arguments:
  -h, --help            show this help message and exit
  -p PROFILE, --profile PROFILE
                        which profile (from ~/.magnetsdk/config) to obtain API
                        key from
  -i, --indent          indent JSON output
  -v, --verbose         set verbose mode
  -o OUTFILE, --outfile OUTFILE
                        destination file to write to, if exists will be
                        overwritten

甚至可以通过提供文件名来使用持久警报迭代器 在列出警报时使用--persist

$ niddel alerts -h
usage: niddel alerts [-h][--start START][-p PERSIST][-f {json,cef}]
                     organization

list an organization's alerts

positional arguments:
  organization          ID of the organization

optional arguments:
  -h, --help            show this help message and exit
  --start START         initial batch date to process in YYYY-MM-DD format
  --cursor CURSOR       latest cursor returned setting the initial batch of alerts
  -p PERSIST, --persist PERSIST
                        file to store persistent state data, to ensure only
                        alerts that haven't been seen before are part of the
                        output
  -f {json,cef}, --format {json,cef}
                        format in which to output alerts

请记住,持久性状态仅在紧接着 在打印完所有未处理的警报后,命令退出。 标准输出。因此,如果cli实用程序中断或发生异常 正在处理,未保存任何状态,此中的任何警报输出均失败 执行不被视为已处理。

警报的默认输出格式是json,但是如果您提供 --format cef然后ArcSight Common Event Format 将改为使用。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
有没有一种方法可以将不同的java web应用程序组合到一个web应用程序(war)中而不相互影响?   java一次屏蔽两位   java如何在多个类上初始化元素?   java在后台服务中处理通知或使用GCM(或其他推送通知服务)   java从const方法调用JNI函数   javascript如何使用函数/方法返回?   Java优化:声明类变量与使用临时变量   java字符算术基数8 vs基数10   Java流收集要存储的对象列表   swing我正在用Java中的keyListener制作一个精灵移动器   在Gradle构建脚本中使用Scala(或java)方法   java Android Mediaplayer下一步按钮不起作用   Java Sound API在播放音频文件后将其锁定   java将变量从外部类传递到内部类的最佳方法   使用play framework的博客web应用程序出现java逻辑错误   java我们可以在Spring批处理中处理大型zip文件吗?   java如何检查JTable的选定行的特定列中的值是否已经在JList中?