cannot initialize mail chimp API in python 3 - python

I am trying to learn the mail chimp API in python 3, but I cannot get it started.
from mailchimp3 import MailChimp
client = MailChimp('MY-USERNAME’,‘MY-API')
(obviously I swapped out my username and api key for this example)
Traceback (most recent call last):
File "/Users/jb/Documents/test2.py", line 3, in <module>
client = MailChimp('MY-USERNAME’,‘MY-API')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mailchimp3/__init__.py", line 96, in __init__
super(MailChimp, self).__init__(*args, **kwargs)
TypeError: __init__() missing 1 required positional argument: 'mc_secret'
I'm very new to python and API's in general, but typically I can find someone else who has had the same error, but all my searches come up blank. I looked in the MailChimp module and I can see that it is suppose to take my API key as the mc_secret argument, so I'm not sure why I keep getting this error. I did just create my mail chimp account today, so perhaps mail chimp takes a while to activate the key or something?

well....I feel sort of stupid.
I just retyped it (instead of copy and pasting it from the documentation) and it worked. I should have noticed that the ',' between the arguments was green in IDLE, indicating something was wrong with the text (encoding or something?). Now it works. Lesson learned, don't just copy and paste from documentation.

Related

"AttributeError: 'module' object has no attribute" - Can someone explain the meaning of this error message?

I'm trying to get the ODB library working. In the documentation at https://python-obd.readthedocs.io/en/latest/ it lists the following code:
import obd
connection = obd.OBD("/dev/ttyUSB0") # connects to USB or RF port
cmd = obd.commands.SPEED # select an OBD command (sensor)
response = connection.query(cmd) # send the command, and parse the response
print(response.value) # returns unit-bearing values thanks to Pint
print(response.value.to("mph")) # user-friendly unit conversions
When I put this in a file called test.py and I run it:
python2 test.py
I get the following error message:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import obd
File "/home/ubuntu/obd.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'OBD'
Stackoverflow comes up with several iterations of this error message, but none clearly explain the problem, only giving specific solutions to those libraries.
I guess it's obvious that I'm new to Python, and I'm having trouble interpreting this error message, even after several hours of writing several small Python programs. I am of course also interested in why the error message is so un-intuitive to a newcomer and where I can gain the common knowledge I might be missing, and I guess this is as good a case to discover that through as any.
So far I have figured out the following:
<module> refers to the name of my python script that I am trying to run.
The mistake I made at first, was to name my test.py file, initially obd.py. This conflicted with the import obd as it was trying to import the file itself - even after I deleted it! The reason was that when I tried to run it the first time, it created a file called obd.pyc - and even though obd.py was no longer there, import OBD found the initially created obd.pyc and tried to import that - which of course does not contain the OBD object from the library I was trying to use.
This is not the detailed answer I'm looking for, so please add a more detailed explanation - or a link to how Python compiling works, if you can.

Google Security Center Python API Failing

I am trying to retrieve findings from Google Security Center using the Python API. I have installed the python libraries, set up a service account, generated a key and when I try to get the finding or any client functions I get the following error:
Traceback (most recent call last):
File "./find.py", line 12, in <module>
finding_result_iterator = client.list_findings(all_sources)
File "/usr/local/lib/python3.6/site-packages/google/cloud/securitycenter_v1/gapic/security_center_client.py", line 1532, in list_findings
self.transport.list_findings,
AttributeError: 'str' object has no attribute 'list_findings'
I am using the code example from here:
https://cloud.google.com/security-command-center/docs/how-to-api-list-findings
Using Python 3.6, I have the json key file in the client create and my organization id. Any idea why I can't get any client functions to work?
I am passing the API Key in as the argument for authentication like this
client = securitycenter.SecurityCenterClient("gcp-sc.json")
If you have a file called gcp-sc.json with the Google credential data, either
set the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to that path, then initialize the client without configuration (SecurityCenterClient()), it'll pick that up
or if you need to explicitly name the file, SecurityCenterClient.from_service_account_json('gcp-sc.json') ought to do the trick.
You can also pass in a custom credentials object (see the docs) as SecurityCenterClient(credentials=...)

Control firewalld in CentOS via Python's dbus module?

My goal is to automate configuring firewalls on CentOS 7 machines using Python.
The OS comes with firewalld, so that's what I'm using. I looked into it and found that it uses dbus (I've never heard of or dealt with any of this - please correct me if anything I say is incorrect.)
I found this documentation for how to control dbus processes using Python:
http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.txt
I checked and the version of Python that comes with the OS includes the dbus module, so it seems like a promising start.
That document suggests that I needed to learn more about what firewalld exposes via the dbus interface. So I did some more research and found this:
https://www.mankier.com/5/firewalld.dbus
The first document says I need to start out with a "well-known name". Their example for such a thing was org.freedesktop.NetworkManager. The second document is titled firewalld.dbus, so I figured that was as good a name as any to try since the document doesn't explicitly give a name anywhere else.
The first document also says I need a name for an object path. Their example is /org/freedesktop/NetworkManager. The second document has an object path of /org/fedoraproject/FirewallD1.
I put those together and tried using the first method the first document suggested, SystemBus's get_object():
>>> from dbus import SystemBus
>>> bus = SystemBus()
>>> proxy = bus.get_object('firewalld.dbus', '/org/fedoraproject/FirewallD1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/dbus/bus.py", line 241, in get_object
follow_name_owner_changes=follow_name_owner_changes)
File "/usr/lib64/python2.7/site-packages/dbus/proxies.py", line 248, in __init__
self._named_service = conn.activate_name_owner(bus_name)
File "/usr/lib64/python2.7/site-packages/dbus/bus.py", line 180, in activate_name_owner
self.start_service_by_name(bus_name)
File "/usr/lib64/python2.7/site-packages/dbus/bus.py", line 278, in start_service_by_name
'su', (bus_name, flags)))
File "/usr/lib64/python2.7/site-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException:
org.freedesktop.DBus.Error.ServiceUnknown:
The name firewalld.dbus was not provided by any .service files
I also gave org.fedoraproject.FirewallD1 a try as the first parameter but ended up with a similar error message.
Why are these not working? Is there some way I can discover what the proper names are? It mentions ".service files" at the end of the error message... where would such a file be located?
Edit: Found several ".service files" by using find / -name *.service. One of them is at /usr/lib/systemd/system/firewalld.service... seems pretty promising so I'll check it out.
Edit 2: It's a rather short file... only about 10 lines. One of them says BusName=org.fedoraproject.FirewallD1. So I'm not sure why it said the name was not provided by any .service files... unless it's not using this file for some reason?
If the unit file says:
BusName=org.fedoraproject.FirewallD1
Then maybe you should try using that as your bus name:
>>> import dbus
>>> bus = dbus.SystemBus()
>>> p = bus.get_object('org.fedoraproject.FirewallD1', '/org/fedoraproject/FirewallD1')
>>> p.getDefaultZone()
dbus.String(u'FedoraWorkstation')
I figured this out based on the fact that this:
>>> help(bus.get_object)
Says that the get_object call looks like:
get_object(self, bus_name, object_path, introspect=True, follow_name_owner_changes=False, **kwargs)

Cannot get any log information from pysimplesoap

I'm trying to use pysimplesoap (v.1.10) and getting what appears to be some kind of parsing error when executing a method request.
Stripped down version:
import pysimplesoap
soapURL = "https://site/path/to/soap"
namespace = "https://site/path/to/ns"
soapClient = pysimplesoap.client.SoapClient(location=soapURL + "?wsdl",namespace=namespace)
response = soapClient.getDocumentContent('1234567')
(python 2.7.8 btw)
Results in an error:
Traceback (most recent call last):
File ".\SOAPtest2.py", line 60, in <module>
response = soapClient.getDocumentContent('1234567')
File "build\bdist.win32\egg\pysimplesoap\client.py", line 139, in <lambda>
AttributeError: 'str' object has no attribute 'call'
However, the real question I have is that I am trying (unsuccessfully) to get logging working, but cannot see any output or determine/confirm what the XML structure it is sending/receiving. I might be able to diagnose the problem if I could see what it is receiving/trying to parse.
I have a gist of the code and the error I'm getting as well.
The odd part is that in my original script (before I stripped down to just some test code) I had a secondary logging instance and file handler and it worked just fine. So it seems specific to the pysimplesoap logging.
Any help would be greatly appreciated.
EDIT: Solution
Per KenK's recommendation, I modified my method call to be (documentId='1234567') and it worked. The script got past that error and I got a few log/debug lines in output. It seems that pysimplesoap simply has so few log/debug lines that none were reached prior to the error I was hitting.
Add the following code to your code:
import logging
logging.basicConfig(level=logging.DEBUG)
To fix the error you're getting, you need to specify an attribute name. Like this:
response = soapClient.getDocumentContent(name='1234567')
Replace name with whatever's defined for this function.

Python RESTful client with CAS authentication

I'm trying to build a python library for interacting with our RESTful API, but it uses CAS for client auth, and I haven't been able to find any good existing libraries for it. So far I've found the following links, but I'm not sure if they're intended to be used in clients or by a website that uses CAS itself. Does anyone have any advice for a good library to use and a good way to structure my code for interacting with it?
https://wiki.jasig.org/download/attachments/28213515/pycas.py.txt
https://sp.princeton.edu/oit/sdp/CAS/Wiki%20Pages/Python.aspx
http://github.com/benoitc/restkit/
http://morethanseven.net/2009/02/18/python-rest-client.html
I also just tried using caslib, but that fails to work:
>>> import caslib
>>> srv = caslib.CASServer('https://my.cas/auth')
>>> svc = caslib.CASService('https://my.service/foo')
>>> caslib.login_to_cas_service(srv.login(svc),'user#example.com','password')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "caslib/cas_dance.py", line 250, in login_to_cas_service
raise CASLoginError('Could not parse the document at %s: %s' % (login_fh.url, errors))
caslib.cas_dance.CASLoginError: Could not parse the document at https://my.cas/auth/login?service=https%3A%2F%2Fmy.service%2Ffoo: undefined entity ©: line 97, column 26
Hmm, the error above appears to be in our markup (or the validater that caslib uses.)
Edit again: The failure is removed after installing the lxml library for python. The fallback parser didn't work as well.
You might have to roll you own solution, either by modifying the python rest client to support CAS, or building something from scratch (I would recommend on top of httplib2.)
Eleven years later, there are at least two Python CAS libraries, with Flask examples available:
python-cas - seems more current
Flask-CAS -- Github repo gone
I don't personally have these working yet, so YMMV.
Maybe, the official python example in: https://wiki.jasig.org/display/casum/restful+api

Categories

Resources