Getting docker stats programmatically - python

I am using docker-py and trying to get docker stats. But I am unable to get any API for returning stats for particular container. Is there a REST API or any other way to programmatically get the stats ?
>>> cli = docker.Client(base_url="tcp://xxxxx:2375", version='1.21')
>>> cli.containers() >> gives the right o/p
>>> cli.containers.get()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'get'
>>> docker.version
'1.10.6'

You may be looking for Container.stats.

Get stats for a particular container name example: "monitoring-tinydb".
Note: change this container name with your desired container id or name.
import docker
client = docker.from_env()
container = client.containers.get("monitoring-tinydb")
status = container.stats(decode=None, stream = False)
print(status)
Get stats for all containers running in the docker host
import docker
client = docker.DockerClient(base_url='unix:///var/run/docker.sock')
for containers in client.containers.list():
print(containers.stats(decode=None, stream = False))

Related

How to start a Plex stream using python-PlexAPI?

I'm making a python script using the module PlexAPI. My goal is to start a stream on the client Chrome. The movie to view has the key /library/metadata/1.
Documentation and sources of code:
playMedia documentation
Example 4 is used but changed to fit my requirements
I'm using the key parameter
from plexapi.server import PlexServer
baseurl = 'http://xxx.xxx.xxx.xxx:xxxxx'
token = 'xxxxx'
plex = PlexServer(baseurl, token)
client = plex.client("Chrome")
client.playMedia(key="/library/metadata/1")
This gives the following error:
Traceback (most recent call last):
File "start_stream.py", line 7, in <module>
client.playMedia(key="/library/metadata/1")
TypeError: playMedia() missing 1 required positional argument: 'media'
So I edit the file:
client.playMedia(key="/library/metadata/1")
#changed to
client.playMedia(key="/library/metadata/1", media="movie")
But then I get a different error:
Traceback (most recent call last):
File "start_stream.py", line 7, in <module>
client.playMedia(key="/library/metadata/1", media="movie")
File "/usr/local/lib/python3.8/dist-packages/plexapi/client.py", line 497, in playMedia
server_url = media._server._baseurl.split(':')
AttributeError: 'str' object has no attribute '_server'
I don't really understand what's going on. Can someone help?
I was able to make it work the following way:
from plexapi.server import PlexServer
baseurl = 'http://xxx.xxx.xxx.xxx:xxxxx'
token = 'xxxxx'
plex = PlexServer(baseurl, token)
key = '/library/metadata/1'
client = plex.client("Chrome")
media = plex.fetchItem(key)
client.playMedia(media)

Shodan - Python Syntax to use "Info" Tag

So I'm currently working on a script using Shodan in Python using import shodan.
The documentation around isn't the best so id thought I'd bring my issue here.
Using the Shodan CLI there is a command known as Shodan Info which displays the number of search credits that your account has remaining.
When looking at the Shodan Documentation i can see that there is an info() tag but whenever I use it there has been an error.
Does anyone know the correct syntax for using info in a python script to display the number of credits an account has?
Errors i have received:
ipinfo = shodan.info()
print (ipinfo)
Error
Traceback (most recent call last):
File "C:/Users/XXXX/OneDrive - XXXX/Documents/XX Documents/XXXX/Working
Segments/ShodanScraper.py", line 8, in <module>
ipinfo = shodan.info()
AttributeError: module 'shodan' has no attribute 'info'
And
ipinfo = shodan.Shodan(info())
print (ipinfo)
Error
Traceback (most recent call last):
File "C:/Users/XXXX/OneDrive - XXXX/Documents/XXXXDocuments/XXXXXX/Working
Segments/ShodanScraper.py", line 8, in <module>
ipinfo = shodan.Shodan(info())
NameError: name 'info' is not defined
You need to instantiate the Shodan class and then you will be able to use its Shodan.info() method. Here is how it looks in the official command-line interface:
https://github.com/achillean/shodan-python/blob/master/shodan/__main__.py#L364
Here's a short script that prints the current account usage information:
import pprint
import shodan
key = 'YOUR API KEY'
api = shodan.Shodan(key)
results = api.info()
pprint.pprint(results)
shodan does not express how to use info. heres a breif example of how to set up shodan info
import shodan
api = shodan.Shodan('YOUR API KEY')
info = api.host('8.8.8.8')
shodan info

'Check' object has no attribute 'image' while using sightengine api

I am using sightengine API to detect child sexual abuse in images. In order to do that I am trying to detect the nudity level in an image using sightengine API. The following example is provided in the documentation itself.
from sightengine.client import SightengineClient
client = SightengineClient('api_user', 'api_key')
output = client.check('nudity').image('https://d3m9459r9kwism.cloudfront.net/img/examples/example7.jpg')
Apart from copying the same code I am getting the following error.
Traceback (most recent call last):
File "driver.py", line 3, in <module>
output = client.check('nudity').image('https://d3m9459r9kwism.cloudfront.net/img/examples/example7.jpg')
AttributeError: 'Check' object has no attribute 'image'
I have used both Python 2 and Python 3 for the same code but both raise the same error.
Try This:
from sightengine.client import SightengineClient
client = SightengineClient('API user', 'API secret')
checkNudity = client.check('nudity')
output1 = checkNudity.set_url('https://d3m9459r9kwism.cloudfront.net/img/examples/example7.jpg')
print(output1)

Using google_rest API

I am trying to use the Google Drive API to download publicly available files however whenever I try to proceed I get an import error.
For reference, I have successfully set up the OAuth2 such that I have a client id as well as a client secret , and a redirect url however when I try setting it up I get an error saying the object has no attribute urllen
>>> from apiclient.discovery import build
>>> from oauth2client.client import OAuth2WebServerFlow
>>> flow = OAuth2WebServerFlow(client_id='not_showing_client_id', client_secret='not_showing_secret_id', scope='https://www.googleapis.com/auth/drive', redirect_uri='https://www.example.com/oauth2callback')
>>> auth_uri = flow.step1_get_authorize_url()
>>> code = '4/E4h7XYQXXbVNMfOqA5QzF-7gGMagHSWm__KIH6GSSU4#'
>>> credentials = flow.step2_exchange(code)
And then I get the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/oauth2client/util.py", line
137, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Library/Python/2.7/site-packages/oauth2client/client.py", line
1980, in step2_exchange
body = urllib.parse.urlencode(post_data)
AttributeError: 'Module_six_moves_urllib_parse' object has no attribute
'urlencode'
Any help would be appreciated, also would someone mind enlightening me as to how I instantiate a drive_file because according to https://developers.google.com/drive/web/manage-downloads, I need to instantiate one and I am unsure of how to do so.
Edit: So I figured out why I was getting the error I got before. If anyone else is having the same problem then try running.
sudo pip install -I google-api-python-client==1.3.2
However I am still unclear about the drive instance so any help with that would be appreciated.
Edit 2: Okay so I figured out the answer to my whole question. The drive instance is just the metadata which results when we use the API to search for a file based on its id
So as I said in my edits try the sudo pip install and a file instance is just a dictionary of meta data.

Error connecting to SF object to get the ForecastingItem object description using beatbox - python

from a ubuntu machine I use beatbox python package to connect to SF and get the objects descriptions
but can't manage to get access to the ForecastingItems object.
I checked my privileges to access the object and I have full access as admin
the script I'm using is bellow, when I changed the object ForecastingItem with Account it does pull all the object fields.
#!/usr/bin/python
# coding=utf8
import beatbox
import pprint
import sys
import os
import datetime
sf_service = beatbox.PythonClient()
sf_service.login('email#hotmail.com', 'password$numbersletter')
desc_obj = sf_service.describeSObjects('ForecastingItem')
forcat_item = desc_obj[0]
forItem_fields = forcat_item.fields
for sf_field_key, sf_field_value in forItem_fields.items():
print sf_field_key
I heard that I should upgrade beatbox to something more than the version 21 to be able to access the ForecastingItem object so I tried apt-get update upgrade beatbox, but I still get the error :
Traceback (most recent call last):
File "./fields_associated_with_an_object.py", line 14, in <module>
desc_obj = sf_service.describeSObjects('ForecastingItem')
File "/usr/local/lib/python2.7/dist-packages/beatbox-20.0-py2.7.egg/beatbox/python_client.py", line 131, in describeSObjects
res = BaseClient.describeSObjects(self, sObjectTypes)
File "/usr/local/lib/python2.7/dist-packages/beatbox-20.0-py2.7.egg/beatbox/_beatbox.py", line 108, in describeSObjects
return DescribeSObjectsRequest(self.__serverUrl, self.sessionId, sObjectTypes).post(self.__conn)
File "/usr/local/lib/python2.7/dist-packages/beatbox-20.0-py2.7.egg/beatbox/_beatbox.py", line 332, in post
raise SoapFaultError(faultCode, faultString)
beatbox._beatbox.SoapFaultError: 'INVALID_TYPE' "INVALID_TYPE: sObject type 'ForecastingItem' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names."
Thanks in advance!

Categories