Wednesday, October 14, 2015

Turn a function in asynchrounous mode
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from threading import Thread
from time import sleep

class async(Thread):

    def __init__(self, callback, errback):
        super(async, self).__init__()
        self.callback = callback
        self.errback = errback

    def __call__(self, func):
        # à l'appel de la fonction, on récupère juste la fonction
        # et ses arguments, et on lance notre thread
        def wrapper(*args, **kwargs):
            self.func = func
            self.args = args
            self.kwargs = kwargs
        return wrapper

    def run(self):
            retval = self.func(*self.args, **self.kwargs)
        except Exception as err:

def my_callback(retval):
    print "CALLBACK:", retval
    # ici on peut faire des opérations supplémentaires
    # sur la valeur renvoyée par la fonction.
    # ce code est appelé par le thread, il ne peut donc
    # pas bloquer notre thread principal

def my_errback(err):
    print "ERRBACK", err
    # ici on peut re-lancer l'exception, ou simplement
    # l'ignorer. Ce code est également appelé dans le thread

@async(my_callback, my_errback)
def ma_fonction_reussie(n):
    return n

@async(my_callback, my_errback)
def ma_fonction_foireuse(n):
    raise AttributeError("Got %s, expected 'foo'" % n)


# histoire de passer le temps pendant ce temps là...
for i in range(10):
    print "MAIN:", i

Remote command with Paramiko-Expect
#!/usr/bin/env python
# PyNet Class Exercises by Nick Ellson
__author__ = "Nick Ellson"
import paramiko
from paramikoe import SSHClientInteraction
import getpass

# Collect an target, user, and password. This example assumes that privilege level 15 is on your Cisco VTY.
ip = input("Enter Host: ")
username = input("Enter Username: ")
password = getpass.getpass()

#Initialize teh Paramiko connection
remote_conn_pre = paramiko.SSHClient()
remote_conn_pre.connect(ip, username=username, password=password, allow_agent=False, look_for_keys=False)
print ("SSH connection established to %s" % ip)

# Here we make a simple Paramiko only shell because we do not yet have a prompt example to work with for Expect.
# So this will basically log us in, and grab that prompt that we land with, and disconnect.
remote_conn = remote_conn_pre.invoke_shell()   
output = remote_conn.recv(1000).decode("utf-8")
prompt = output.strip()

# Now we reconnect with the paramiko expect module and we have a nice interactive conversation.
remote_conn_pre.connect(ip, username=username, password=password, allow_agent=False, look_for_keys=False)
interact = SSHClientInteraction(remote_conn_pre, timeout=20, display=False)

#Logged in, wait for the prompt to display.
#Shut off the --more-- prompting
interact.send('terminal length 0')
#We don't care about the output for that last command, so just clear the buffer.
cmd_output = interact.current_output_clean
#Let's grab something BIG that use dto be a real timing issue the old way, not knowing how long it might take to get a full 
# running config over a WAN link. Now we do not care, we will simply wait to the prompt shows up. 
interact.send('show running-config')
cmd_output = interact.current_output_clean
print (cmd_output)

#Close our session

Monday, October 12, 2015

Spnner - AsyncIO example-

Nice example using asyncio in Python3.5

# credits: Example by Luciano Ramalho inspired by
# Michele Simionato's multiprocessing example in the python-list:

import asyncio
import itertools
import sys

async def spin(msg):  # <1>
    write, flush = sys.stdout.write, sys.stdout.flush
    for char in itertools.cycle('|/-\\'):
        status = char + ' ' + msg
        write('\x08' * len(status))
            await asyncio.sleep(.1)  # <3>
        except asyncio.CancelledError:  # <4>
    write(' ' * len(status) + '\x08' * len(status))

async def slow_function():  # <5>
    # pretend waiting a long time for I/O
    #await asyncio.sleep(3)  # <6>
    for i in range(100000000):
        i += 1
        await asyncio.sleep(.0000001)
    return 42

async def supervisor():  # <7>
    spinner = asyncio.ensure_future(spin('thinking!'))  # <8>
    print('spinner object:', spinner)  # <9>
    result = await slow_function()  # <10>
    spinner.cancel()  # <11>
    return result

def main():
    loop = asyncio.get_event_loop()  # <12>
    result = loop.run_until_complete(supervisor())  # <13>
    print('Answer:', result)

if __name__ == '__main__':