与NearlyFreespeech的API交互

python-nfsn的Python项目详细描述


python nfsn

https://travis-ci.org/ktdreyer/python-nfsn.svg?branch=masterhttps://coveralls.io/repos/ktdreyer/python-nfsn/badge.svg?branch=master&service=githubhttps://badge.fury.io/py/python-nfsn.svg

一个现代的python库,用于访问nearlyfreespeech.net的api。

  • 干净的pythonic api
  • 支持100%的NFSN API(从“今日起”:)
  • 支持Python2.6到3.5
  • 良好的测试覆盖率(使用httpretty
  • 使用random.SystemRandom()
  • 生成加密安全的salt

安装

要快速运行,请从pypi安装:

pip install python-nfsn

这将在您的$PATH中放置一个pynfsnuility。

如果您想破解代码:

git clone https://github.com/ktdreyer/python-nfsn
cd python-nfsn
virtualenv venv
. venv/bin/activate
python setup.py develop

快速启动

通过通过 控制面板,并将其存储在主目录中的json文件中,如下所示:

$ cat ~/.nfsn-api
{ "login": "ktdreyer",  "api-key": "aGVsbG90aGVyZWZyaWVuZA" }

使用pynfsn命令行实用程序,这是一个可以让您探索 API

$ pynfsn member ktdreyer accounts
[u'D41D-8CD98F00', u'B204-E9800998', u'ECF8-427E6D7F']

$ pynfsn member ktdreyer sites
[u'coolsite', u'anothercoolsite']

$ pynfsn dns example.com listRRs www
[{u'data': u'example.nfshost.com.', u'scope': u'system', u'type': u'CNAME', u'name': u'www', u'ttl': u'600'}]

$ pynfsn dns example.com addRR testing A 192.0.2.2

$ pynfsn dns example.com removeRR testing A 192.0.2.2

或者直接在自己的代码中使用api:

fromnfsnimportNfsnnfsn=Nfsn(login='myusername',api_key='aGVsbG90aGVyZWZyaWVuZA')rrs=nfsn.dns('example.com').listRRs(name='www')forrrinrrs:print(rr['name'])# eg. 'www'print(rr['type'])# eg. 'A'print(rr['data'])# eg. '192.0.2.2'

身份验证

有三种方法可以将身份验证凭据传递给Nfsn() 构造器:

  1. 调用不带参数的构造函数:

    n=Nfsn()

    默认情况下,库将查找一个$HOME/.nfsn-apijson文件 包含您的用户名和API密钥,例如:

    $ cat ~/.nfsn-api
    { "login": "ktdreyer",  "api-key": "aGVsbG90aGVyZWZyaWVuZA" }
    

    (这与perl nfsn api用于 顺便说一下,身份验证。)

  2. 使用api密钥登录文件的显式路径调用构造函数:

    n=Nfsn(login_file='/etc/nfsn-api')

    在本例中,login_file应该是一个json文件,类似于 上面的例子。

  3. 直接指定登录字符串和API密钥字符串。您可以跳过json 完全登录文件,只需传递所需的字符串即可:

    n=Nfsn(login='ktdreyer',api_key='aGVsbG90aGVyZWZyaWVuZA')

如果未输入正确的登录名和密钥组合,则每次访问 使用此库的属性或方法(见下文),nearlyfreespeech.net将 返回一个http 401错误,这个库将引发一个RuntimeError

API示例

有关详细信息,请参见https://members.nearlyfreespeech.net/wiki/API

账户api

fromnfsnimportNfsnnfsn=Nfsn(login='ktdreyer',api_key='aGVsbG90aGVyZWZyaWVuZA')# A floating-point value, the balance on the account.# Example: 9.04nfsn.account('A1B2-C3D4E5F6').balance# The friendly, human-readable name for an account.# Example: "Personal" or "Business"nfsn.account('A1B2-C3D4E5F6').friendlyNamenfsn.account('A1B2-C3D4E5F6').friendlyName='Business'# The status details for an account.# Example: { 'color': '#00b000', 'short': 'OK', 'status': 'Ok' }# (Note: returns an AttrDict)nfsn.account('A1B2-C3D4E5F6').status# The sites associated with an account.# Example: [ 'coolsite', 'anothercoolsite' ]nfsn.account('A1B2-C3D4E5F6').sites# Add a new site to an account.nfsn.account('A1B2-C3D4E5F6').addSite(site='testing')# Add a new warning to an account.nfsn.account('A1B2-C3D4E5F6').addWarning(balance=1.23)# Remove a warning from an account.nfsn.account('A1B2-C3D4E5F6').removeWarning(balance=1.23)

DNS API

fromnfsnimportNfsnnfsn=Nfsn(login='ktdreyer',api_key='aGVsbG90aGVyZWZyaWVuZA')# Get or set the expiration value for a DNS zone.nfsn.dns('example.com').expire# Example: 86400nfsn.dns('example.com').expire=86401# Get the minTTL value for a DNS zone.# Example: 180nfsn.dns('example.com').minTTL# Get the minTTL value for a DNS zone.# Example: 600nfsn.dns('example.com').refresh# Get the retry value for a DNS zone.# Example: 180nfsn.dns('example.com').retry# Get the serial value for a DNS zone.# Example: 1414129428nfsn.dns('example.com').serial# Add a DNS resource record. The name+type must not exist yet.nfsn.dns('example.com').addRR(name='testing',type='A',data='192.0.2.2')# List all DNS resource records:# (Note: returns an AttrDict)# Example:#    [{'data':  '192.0.2.1',#      'name':  '',#      'scope': 'member',#      'ttl':   '3600',#      'type':  'A'},#     {'data':  'ns.phx2.nearlyfreespeech.net.',#      'name':  '',#      'scope': 'member',#      'ttl':   '3600',#      'type':  'NS'}]nfsn.dns('example.com').listRRs()# List all DNS resource records for 'www.example.com':# (Note: returns an AttrDict)# Example:#    [{'data':  '192.0.2.1',#      'name':  'www',#      'scope': 'member',#      'ttl':   '3600',#      'type':  'A'}]nfsn.dns('example.com').listRRs(name='www')# Add a DNS resource record.# The name+type must exist, or Nfsn will raise an an error. You must# specify all three parameters (name, type, data).nfsn.dns('example.com').removeRR(name='testing',type='A',data='192.0.2.2')

电子邮件api

fromnfsnimportNfsnnfsn=Nfsn(login='ktdreyer',api_key='aGVsbG90aGVyZWZyaWVuZA')# List all email forwarding.# Example: { 'hello': 'customerservice@example.net'}# (Note: returns an AttrDict)nfsn.email('example.com').listForwards()# Forward all 'hi@example.com' mail to 'h@example.net':nfsn.email('example.com').setForward(forward='hi',dest_email='h@example.net')# ... And remove the email forward:nfsn.email('example.com').removeForward(forward='hi')

成员API

fromnfsnimportNfsnnfsn=Nfsn(login='ktdreyer',api_key='aGVsbG90aGVyZWZyaWVuZA')# Get a list of all accounts belonging to a member.# Example: [ 'A1B2-C3D4E5F6' ]nfsn.member('ktdreyer').accounts# Get a list of all sites belonging to a member.# Example: [ 'coolsite', 'anothercoolsite' ]nfsn.member('ktdreyer').sites

站点api

fromnfsnimportNfsnnfsn=Nfsn(login='ktdreyer',api_key='aGVsbG90aGVyZWZyaWVuZA')# Add or remove an alias for a site:nfsn.site('mycoolsite').addAlias(alias='mobile.example.com')nfsn.site('mycoolsite').removeAlias(alias='mobile.example.com')

类型和错误

注意,因为我们使用Beanbag 在内部,当我们返回dict值时,它实际上是一个AttrDict。如果要将值转换为 一个普通的dict,您需要使用+运算符。在值前面加上 +符号,例如:

rrs=nfsn.dns('example.com').listRRs()print+rrs

如果您尝试访问不存在的属性或方法,NearlyFreeSpeech.net 将返回http 404 not found错误,此库将引发 BeanBagException

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

推荐PyPI第三方库


热门话题
用户界面java,使用gui连接到另一台计算机/服务器的文件系统   运行sbt的ubuntu返回错误:“javahome需要<path>参数”   java如何在Android中处理许多ImageView而不出现内存问题?   查询中非法字符的java相同URL失败   安卓取消引用可能会产生“java”。lang.NullPointerException'   java中的indexoutofboundsexception“java.lang.ArrayIndexOutOfBoundsException”错误   xml Java将dom保存到文件>文件在程序结束后由另一个进程打开   Java的垃圾收集器是如何工作的?   Java如何筛选值(列表)   java处理字符串我怎样才能像在真实的书籍中一样在上面部分生成“小数字”呢?   java SonarQube是否有一个API来获取所有项目分析的一部分?   java startActivity(intent)什么都不做   JAVAutil。扫描器类Java   java如何从Firebase更新电子邮件?UpdateMail方法已被弃用   java Hibernate。如何正确组织带有注释的onetomany关系?   在java中获得卷标和驱动器号之间的映射(而不是FileSystemView)的解决方法是什么   java查找文件的路径