The problem of using python's paramiko package

Keywords: pip Python

Problems encountered on March 14, 2017.

In fact, two problems are mixed together. The first one is:

No handlers could be found for logger "paramiko.transport"

This problem is mixed with the second one below.

The root of the problem is that the tornado logger is used in the test code. The root logger does not set handlers, but the'tornado.application'logger sets handlers (Stream Handler), so you need to modify the test code to set the default root handlers as NullHandler and the'tornado.application' logger as Stream Handler. Refer to test.py in sysmgt.

 

The second problem emerged after fixing the first one. The error was:

D:\temp\py>python paramiko_test.py
test paramiko ........
time1 : [0.0] - 10.99.201.174 - wcadmin
time1 : [0.0] - 10.122.2.6 - wcadmin
time2 : [0.0] - 10.99.201.174 - wcadmin
time2 : [0.0] - 10.122.2.6 - wcadmin
time3 : [0.0] - 10.99.201.174 - wcadmin
time3 : [0.0] - 10.122.2.6 - wcadmin
time4 : [0.0] - 10.99.201.174 - wcadmin - 
time4 : [0.0] - 10.122.2.6 - wcadmin - 
--- ip=10.99.201.174, username=wcadmin, password=
--- ip=10.122.2.6, username=wcadmin, password=
Traceback (most recent call last):
  File "paramiko_test.py", line 42, in one_connection
    sshclient.connect(ip, 22, username, password)
  File "C:\app\Python27\lib\site-packages\paramiko\client.py", line 338, in connect
    t.start_client(timeout=timeout)
  File "C:\app\Python27\lib\site-packages\paramiko\transport.py", line 500, in start_client
    raise e
RequirementParseError: Invalid requirement, parse error at "''"
time5 : [0.452000141144] - 10.99.201.174 - wcadmin
time6 : [0.46799993515] - 10.99.201.174 - wcadmin

Above is the test result of the test program (multithreaded connection to remote server). The same program is okay with my own computer.

Requirement ParseError: The problem of Invalid requirements is not that every time a test program is run, the probability of error is 85%.

Solution:

I suspect the problem of outdated PIP version, and finally uninstall it and install it. Because the old version of PIP was not found, the PIP was new both before and after. It turned out that this had nothing to do with pip.

2 suspect the version problem of paramiko, upgrade paramiko from 1. * to 2 +.

3 According to the similar problems on the internet, it is the problem of setuptools library, uninstall, install and report that the module appdirs can not be found. Download and install the module appdirs. Then install a new version of setuptools to solve this problem.

The old setuptools version is 22.0.0, while the new one is 34.3.2. My version is 16.0. So it seems that a version of setuptools conflicts with paramiko.

So it seems that Python libraries may conflict and require multiple environmental tests.

 

My test program:

#-*-coding:UTF-8-*-

__author__='zhaoxp'

import traceback
import logging
import time
import threading

import paramiko

logging.basicConfig(level=logging.DEBUG)
logging.getLogger().handlers=[logging.NullHandler()]
logger = logging.getLogger('paramiko.test')
logger.handlers.append(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

def main():
    threads = []
    threads.append(threading.Thread(target=one_connection, args=('10.99.201.174', 'wcadmin', 'password')))
    threads.append(threading.Thread(target=one_connection, args=('10.122.2.6', 'wcadmin', 'password')))
    threads.append(threading.Thread(target=one_connection, args=('10.99.201.37', 'wcadmin', 'password')))
    threads.append(threading.Thread(target=one_connection, args=('10.99.244.121', 'wcadmin', 'password')))
    for th in threads:
        th.start()
    for th in threads:
        th.join()


def one_connection(ip, username, password):
    sshclient = None
    try:
        start_time = time.time()
        logger.debug('time1 : [%s] - %s - %s'%(time.time()-start_time, ip, username))
        sshclient=paramiko.SSHClient()
        logger.debug('time2 : [%s] - %s - %s'%(time.time()-start_time, ip, username))
        sshclient.load_system_host_keys()
        logger.debug('time3 : [%s] - %s - %s'%(time.time()-start_time, ip, username))
        sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        logger.debug('time4 : [%s] - %s - %s - %s'%(time.time()-start_time, ip, username, password))
        logger.debug('--- ip=%s, username=%s, password=%s'%(ip, username, password))
        sshclient.connect(ip, 22, username, password)
        logger.debug('time5 : [%s] - %s - %s'%(time.time()-start_time, ip, username))
        stdin, stdout, stderr = sshclient.exec_command('pwd')
        #logger.debug('stdout:\n%s \n stderr:\n%s \n'%(stdout.read(), stderr.read()))
        logger.debug('time6 : [%s] - %s - %s'%(time.time()-start_time, ip, username))
    except BaseException as be:
        traceback.print_exc()
    finally:
        if sshclient is not None:
            sshclient.close()

if __name__=='__main__':
    print 'test paramiko ........'
    main()

Posted by Aaron_Escobar on Thu, 18 Apr 2019 12:24:35 -0700