找不到xmlrpclib错误'模块'当试图访问gandi api时

时间:2015-06-13 03:32:12

标签: python api python-3.x xml-rpc

我正在尝试在gandi api support doc

中描述的gandi api中设置DS记录

它声明要导入xmlrpclib'但我立即收到错误'模块未找到' (全文转载如下)。

found this page他们使用xmlrpc导入客户端',但在我的上下文中,gandi API没有响应。

python3 session:

>>> import xmlrpclib
Traceback (most recent call last):
  File "<pyshell#69>", line 1, in <module>
    import xmlrpclib
ImportError: No module named 'xmlrpclib'
>>> from xmlrpc import client
>>> api = xmlrpclib.ServerProxy('https://rpc.gandi.net/xmlrpc/')
Traceback (most recent call last):
  File "<pyshell#71>", line 1, in <module>
    api = xmlrpclib.ServerProxy('https://rpc.gandi.net/xmlrpc/')
NameError: name 'xmlrpclib' is not defined
>>> api = xmlclient.ServerProxy('https://rpc.gandi.net/xmlrpc/')
Traceback (most recent call last):
  File "<pyshell#72>", line 1, in <module>
    api = xmlclient.ServerProxy('https://rpc.gandi.net/xmlrpc/')
NameError: name 'xmlclient' is not defined
>>> api = client.ServerProxy('https://rpc.gandi.net/xmlrpc/')
>>> apikey = '<my_api_key_here>'
>>> version = api.version.info(apikey)
>>> print(version)
{'api_version': '3.3.29'}
>>> domain.dnssec.create('KEY', 'domain2.com', 1, 256, 'KEY')
Traceback (most recent call last):
  File "<pyshell#77>", line 1, in <module>
    domain.dnssec.create('KEY', 'domain2.com', 1, 256, 'KEY')
NameError: name 'domain' is not defined
>>> client.dnssec.create('KEY', 'domain2.com', 1, 256, 'KEY')
Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    client.dnssec.create('KEY', 'domain2.com', 1, 256, 'KEY')
AttributeError: 'module' object has no attribute 'dnssec'

如何通过gandi API更新DS记录?

更新: 我提供了以下代码,错误包含无效错误。 我使用的密钥在PhP中很有名。

my_in_file = open('/home/ex-mailer-domains/'+domain+'/dsset-'+domain+'.', 'rt')
dstext = my_in_file.read()
dslist = dstext.split()[3:7]
print(dslist[3])

api = client.ServerProxy('https://rpc.gandi.net/xmlrpc/')
apikey = 'MyKeyThatWorksInPHPjustFineForOtherScripts'
api.domain.dnssec.create(apikey, 'domain2.com', {'algorithm': 8, 'flags': 256, 'public_key': dslist[3]})





xmlrpc.client.Fault: <Fault 510150: 'Error on object : OBJECT_ACCOUNT (CAUSE_NORIGHT) [Invalid API key]'>

更新: 密钥适用于此示例代码

但是在其他调用中使用它,我仍然会收到错误:

>>> api = client.ServerProxy('https://rpc.gandi.net/xmlrpc/')
>>> apikey = 'MyKeyThatWorksInPHPjustFineForOtherScripts'
>>> version = api.version.info(apikey)
>>> print(version)
{'api_version': '3.3.29'}
>>> domain.dnssec.create('xxxxx', 'domain2.com', 1, 256, '97BF8186C193EBF12A794A75FEF4B331A29C1A26')

2 个答案:

答案 0 :(得分:1)

我不确定您是否确实提供了真正的API密钥,但我建议您尽快更改密钥。在任何情况下,在Python中,顶级变量/名称不是来自空洞 - 它们是由import或赋值语句(x = 123)引入的,(或其他一些语句)通常defclass

你从未在domain变量中分配任何内容,那么它就无法工作;同样适用于xmlrpclib;由于import语句失败,因此未定义名称。

那么怎么样:

>>> from xmlrpc import client
>>> api = client.ServerProxy('https://rpc.gandi.net/xmlrpc/')
>>> apikey = '123'
>>> api.domain.dnssec.create(apikey, 'domain2.com', {'algorithm': 42, 
                             'flags': 123, 'public_key': 'foobarbaz'})

答案 1 :(得分:1)

xmlrpclib模块已在Python 3中重命名为xmlrpc.client。在将源代码转换为Python 3时,2to3工具将自动调整导入。