k1lib.kcom module

This module is for communication with hardware. This is exposed automatically with:

from k1lib.imports import *
kcom.Gsm # exposed
class k1lib.kcom.Gsm(device='/dev/ttyUSB0')[source]

Bases: object

__init__(device='/dev/ttyUSB0')[source]

Communicates with a GSM module using AT codes via a serial device. Example:

g = kcom.Gsm("/dev/ttyUSB0")
g # display in cell, will show basic info

g.sendSms("+1215123456", "some message")
s.readSms()
sendRaw(data: bytes)[source]

Sends data to the module and read response until ‘OKn’ is received

send(data: bytes)[source]

Similar to sendRaw(), but this time clean up control signals like ‘OKn’

sendSms(number: str, data: bytes)[source]

Sends text message to some number.

readSms(mode=1)[source]

Reads text messages.

Parameters

mode – 0 for all, 1 for unread, 2 for read

close()[source]
class k1lib.kcom.Host(user=None, host=None, container=None, verbose=False, password=None, stripPredicates=[])[source]

Bases: object

__init__(user=None, host=None, container=None, verbose=False, password=None, stripPredicates=[])[source]

Represents another computer that you might want to execute commands in. Examples:

h = kcom.Host("username", "hostname")
h.execPy("print('something')", fns=["~/.bashrc"], rmFn=False) # returns [List[bytes], List[bytes], List[bytes]]

There are several available modes:

h = kcom.Host(user="username", host="hostname")     # Mode 1: executes in ssh host
h = kcom.Host(container="nginx-1")                  # Mode 2: executes in container on localhost
h = kcom.Host(host="hostname", container="nginx-1") # Mode 3: executes in container on remote ssh host

Mode 1 and 2 are relatively straightforward and airtight. Mode 3 is a little buggy in stdout and stderr. Once you have constructed them, you can execute random python/bash scripts in the remote host:

h.execPy("import os; print(os.environ)")

Main value of this class is that it can execute random scripts in a convenient manner. I’ve found myself needing this way more often than expected.

Parameters
  • user – username of the ssh host

  • host – host name of the ssh host

  • container – container name. If specified will run commands inside that container

  • verbose – if True, will print out detailed commands that’re executed and their results

  • password – if True, will try to login to ssh host using a password instead of the default key file

  • stripPredicates – list of predicates that if match, will delete those first few lines in stdout and stderr. This is to strip away annoying boilerplate messages that I couldn’t quite get rid of myself

execPy(c: str, fns=None, rmFn=False, pyExec=None)[source]

Executes some python code. Examples:

If .fns is not specified, then will return (List[bytes], List[bytes]) containing (stdout, stderr).

If .fns is specified, then will return (List[bytes], List[bytes], List[bytes], …) containing (stdout, stderr, fn1, fn2, …). Each file is a List[bytes], with endline byte at the end of each bytes chunk

Parameters
  • c – Python commands

  • fns – file names to retrieve. Don’t use paths that can expand, like “~/.bashrc” or “$HOME/.bashrc”, they won’t be expanded

  • rmFn – if True, removes the files after running and exiting, else don’t remove the files. Some scripts auto generates files that should be removed, but others don’t

  • pyExec – py executable used to run the generated python file, like “/usr/bin/python”. If not specified will try to auto detect what python binaries are available

execSh(c: str, fns=None, rmFn=False, shExec='/bin/bash')[source]

Executes some bash code. Pretty much the same as execPy(), but for bash. See that method’s docs.