NameError: name 'infusionsoft' is not defined

import xmlrpc.client

server = xmlrpc.client.ServerProxy("https://xxxxx.infusionsoft.com:443/api/xmlrpc");
key = "xxxxx";

table = 'Contact'
returnFields = ['Id', 'FirstName']
limit = 10
page = 0
print(infusionsoft.DataService('query', table, limit, page, query, returnFields))

I keep on getting the following error when running the query above:

print(infusionsoft.DataService('query', table, limit, page, query, returnFields))
NameError: name 'infusionsoft' is not defined

when I define infusionsoft I then get another error:

from xmlrpclib import ServerProxy, Fault
ModuleNotFoundError: No module named 'xmlrpclib'

Where have I gone wrong?

Another API team member suggests you might be mixing Python versions. Would you check?

The xmlrpclib module has been renamed to xmlrpc.client in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
https://docs.python.org/2/library/xmlrpclib.html

Thanks you ever so much for the reply Mike. I have modified the code slightly but still getting an ‘Invalid syntax’ error at the very last statement. Is there anyone on the team that could suggest a fix? I am just trying to list ALL my contacts and their Phone1 and Phone2 numbers.

import xmlrpc.client

server = xmlrpc.client.ServerProxy(“https://xxxxx.infusionsoft.com:443/api/xmlrpc”);
key = “xxxxx”;

print ("Welcome! You are now connected to: ", server);

table = 'Contact’
returnFields = [‘Id’, ‘FirstName’, ‘Phone1’, ‘Phone2’]
query = {‘FirstName’ : ‘’, ‘’, ‘’}
limit = 10
page = 0
print(server.system.(‘query’, table, limit, page, query, returnFields))

UPDATE: I have amended the code to:

import xmlrpc.client

server = xmlrpc.client.ServerProxy("https://xxxx.infusionsoft.com:443/api/xmlrpc");
key = "xxxxx";
print ("Welcome! You are now connected to: ", server);


table = 'Contact'
returnFields = ['Id','FirstName', 'Phone1', 'Phone2']
query = {'FirstName', 'Phone1', 'Phone2'}
limit = 10
page = 0
results = server.system('query', table, limit, page, query, returnFields)

for result in results:
    print("Found: {},{},{}".format(result['FirstName'], result['Phone1'], result['Phone2']))

But now I keep on getting an error:

Traceback (most recent call last):
  File "C:\...xxxxxAppData\Local\Programs\Python\Python36\lib\xmlrpc\client.py", line 510, in __dump
    f = self.dispatch[type(value)]
KeyError: <class 'set'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:...Test.py", line 15, in <module>
    results = server.system('query', table, limit, page, query, returnFields)
  File "C:\...xxxx\AppData\Local\Programs\Python\Python36\lib\xmlrpc\client.py", line 1112, in __call__
    return self.__send(self.__name, args)
  File "C:\...xxxx\AppData\Local\Programs\Python\Python36\lib\xmlrpc\client.py", line 1446, in __request
    allow_none=self.__allow_none).encode(self.__encoding, 'xmlcharrefreplace')
  File "C:\...xxxx\AppData\Local\Programs\Python\Python36\lib\xmlrpc\client.py", line 971, in dumps
    data = m.dumps(params)
  File "C:\...xxxxxAppData\Local\Programs\Python\Python36\lib\xmlrpc\client.py", line 502, in dumps
    dump(v, write)
  File "C:\...xxxxx\AppData\Local\Programs\Python\Python36\lib\xmlrpc\client.py", line 514, in __dump
    raise TypeError("cannot marshal %s objects" % type(value))
TypeError: cannot marshal <class 'set'> objects

Any suggestions on how to fix this would be greatly appreciated.

Python is not my forte. I found this; does it help?

Thanks Mike. I will look into it.

Is what you’ve posted all your code? I don’t see where you’re using the key variable you’ve declared there.

Hi Chuck, Yes that is the whole code.

Chuck, what should I modify to get a list of all my contacts? Many thanks.

I’d recommend going back and looking really closely at whatever example you copied this code from. There’s a very good chance some of the code got lost at some point.

Python isn’t really a language I focus on right now, sorry. I’ve got some experience with it, but I intentionally avoided XMLRPC as much as possible.

Maybe you could try one of the open source api libraries that are out there-- they tend to make this kind of thing much easier. Do you know how to use pip?

Thanks Chuck. I have pip installed. I used it to install Python packages.


I found this link to be super helpful.

It looks like you need to import infusionsoft.