Chinaunix首页 | 论坛 | 博客
  • 博客访问: 59763
  • 博文数量: 17
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-07 11:14
个人简介

。roth lower(substr(ename,2,length(ena)))

文章分类

全部博文(17)

文章存档

2017年(14)

2013年(3)

我的朋友

分类: Python/Ruby

2017-02-19 18:56:15


一,类的自动进出

点击(此处)折叠或打开

  1. class sth(object):
  2.     def __init__(self,hehe):
  3.         self.a = hehe
  4.     def __enter__(self):
  5.         print "进入类中"
  6.         return self.a
  7.     def __exit__(self, exc_type, exc_val, exc_tb):
  8.         print "退出类"
  9. with sth() as s: # s为 enter的返回
  10.     print s

二, tread 与 treading


点击(此处)折叠或打开

  1. import thread,threading
  2. def print_time(threadName, delay):
  3.     count = 0
  4.     while count < 5:
  5.         time.sleep(delay)
  6.         count += 1
  7.         print "%s : %s" % (threadName, time.ctime(time.time()))

  8. try:
  9.     threading._start_new_thread(print_time, ("Thread-1", 2))
  10.     thread.start_new_thread(print_time, ("Thread-2", 4))
  11. except:
  12.     print "Error:unable to start thread"
  13. while 1:
  14.     pass

三,SSH 链接管理 paramiko

C:\>pip install paramiko
Collecting paramiko
  Downloading paramiko-2.1.1-py2.py3-none-any.whl (172kB)
    100% |████████████████████████████████| 174kB 140kB/s

Successfully installed cffi-1.9.1 cryptography-1.7.2 enum34-1.1.6 idna-2.2 ipaddress-1.0.18 paramiko-2.1.1 pyasn1-0.2.2 pycparser-2.17


点击(此处)折叠或打开

       import paramiko
  1. def ssh2(ip,user,passwd,cmd):
  2.     try:
  3.         ssh = paramiko.SSHClient()
  4.         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  5.         ssh.connect(ip,22,user,passwd,timeout=5)
  6.         stdin,stdout,stderr = ssh.exec_command(cmd)
  7.         stdin.write("Y") #简单交互输入
  8.         print stdout.read()
  9.         for x in stdout.readlines():
  10.             print x.strip("\n")
  11.         print '%s\tOK\n'%(ip)
  12.         ssh.close()
  13.     except:
  14.         print '%s\tError\n'%(ip)

  15. #ssh2()
  16. con = ssh2('172.18.107.223','root','xxx',"hostname")

  17. 结果:
  18. Connected to pydev debugger (build 143.1919)
  19. linux-SUSE

  20. 172.18.107.223    OK


  21. 进程已结束,退出代码0







阅读(493) | 评论(0) | 转发(0) |
0

上一篇:py Note 之format, 装饰器

下一篇:时间处理

给主人留下些什么吧!~~