Python Forum
Flask with paramiko based ssh client gives gevent LoopExit exception
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask with paramiko based ssh client gives gevent LoopExit exception
#4
Changed code to directly use paramiko.

#!usr/bin/python3.6
#from gevent import monkey; monkey.patch_all()
import flask
from flask import request
import paramiko

app = flask.Flask(__name__)
app.config["DEBUG"] = False

def exucute_cmd(cmd,ip,user,pw,port=22):
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(ip, port, user, pw)
    shell = client.invoke_shell()
    _, stdout, stderr = client.exec_command(cmd)
    out=''
    for line in stdout.readlines():
        out+=line
    if not out:
        for line in stderr.readlines():
            out+=line

    shell.close()
    client.close()
    return out

def checkcpu(ip):
    cmd_cpu_usage = '''awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1) "%"; }' <(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)'''
    return exucute_cmd(cmd_cpu_usage,ip,'user','*******')
Getting Error on Paramiko Import:

Error:
Traceback (most recent call last): File "src/gevent/_waiter.py", line 116, in gevent.__waiter.Waiter.switch AssertionError: Can only use Waiter.switch method from the Hub greenlet 2018-12-25T07:30:17Z <io at 0x7f929b9716c8 fd=24 events=READ active callback=<bound method Waiter.switch of <gevent.__waiter.Waiter object at 0x7f929b98df98>> args=(<gevent.__waiter.Waiter object at 0x7f929b98df98>,)> failed with AssertionError mod_wsgi (pid=6149): Failed to exec Python script file '/var/www/az/az.wsgi'. mod_wsgi (pid=6149): Exception occurred processing WSGI script '/var/www/az/az.wsgi'. Traceback (most recent call last): File "/var/www/az/az.wsgi", line 8, in <module> from test import app as application File "/var/www/az/test.py", line 4, in <module> import paramiko File "/usr/local/lib/python3.6/dist-packages/paramiko/__init__.py", line 22, in <module> from paramiko.transport import SecurityOptions, Transport File "/usr/local/lib/python3.6/dist-packages/paramiko/transport.py", line 89, in <module> from paramiko.dsskey import DSSKey File "/usr/local/lib/python3.6/dist-packages/paramiko/dsskey.py", line 27, in <module> from cryptography.hazmat.primitives.asymmetric.utils import ( File "/usr/local/lib/python3.6/dist-packages/cryptography/hazmat/primitives/asymmetric/utils.py", line 9, in <module> from asn1crypto.algos import DSASignature File "/usr/local/lib/python3.6/dist-packages/asn1crypto/algos.py", line 24, in <module> from ._int import fill_width File "/usr/local/lib/python3.6/dist-packages/asn1crypto/_int.py", line 56, in <module> from ._perf._big_num_ctypes import libcrypto File "/usr/local/lib/python3.6/dist-packages/asn1crypto/_perf/_big_num_ctypes.py", line 35, in <module> libcrypto_path = find_library(b'crypto' if sys.version_info < (3,) else 'crypto') File "/usr/lib/python3.6/ctypes/util.py", line 313, in find_library return _findSoname_ldconfig(name) or \\ File "/usr/lib/python3.6/ctypes/util.py", line 282, in _findSoname_ldconfig env={'LC_ALL': 'C', 'LANG': 'C'}) as p: File "/usr/local/lib/python3.6/dist-packages/gevent/subprocess.py", line 619, in __init__ restore_signals, start_new_session) File "/usr/local/lib/python3.6/dist-packages/gevent/subprocess.py", line 1474, in _execute_child data = errpipe_read.read() File "/usr/local/lib/python3.6/dist-packages/gevent/_fileobjectposix.py", line 113, in readall data = self.__read(DEFAULT_BUFFER_SIZE) File "/usr/local/lib/python3.6/dist-packages/gevent/_fileobjectposix.py", line 108, in __read self.hub.wait(self._read_event) File "src/gevent/_hub_primitives.py", line 46, in gevent.__hub_primitives.WaitOperationsGreenlet.wait File "src/gevent/_hub_primitives.py", line 55, in gevent.__hub_primitives.WaitOperationsGreenlet.wait File "src/gevent/_waiter.py", line 151, in gevent.__waiter.Waiter.get File "src/gevent/_greenlet_primitives.py", line 59, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch File "src/gevent/_greenlet_primitives.py", line 59, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch File "src/gevent/_greenlet_primitives.py", line 63, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch File "src/gevent/__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch gevent.exceptions.LoopExit: This operation would block forever \tHub: <Hub '' at 0x7f929cbe9b70 epoll default pending=0 ref=0 fileno=18 thread_ident=0x7f92b8dd4780> \tHandles: []
tried all permutation of import order and also tried monkey patching still getting the error.
The code is working on development server with same python,flask and paramiko versions.
Probably it has something to do with wsgi module?
Reply


Messages In This Thread
RE: Flask with paramiko based ssh client gives gevent LoopExit exception - by hbknjr - Dec-25-2018, 07:48 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020