python中常用到的模組和包名稱

來源:互聯網
上載者:User

標籤:python 常用模組

1 paramiko  (基於openssh,python封裝的ssh)

模組python內建


用法:

import paramiko                                          ssh = paramiko.SSHClient()                               ssh.load_system_host_keys()                              ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(hostname=‘192.168.100.20‘,port=58422,username=‘oldboy‘)stdin, stdout,stderr=ssh.exec_command(‘uptime‘)                    type(stdout)paramiko.ChannelFileprint stderr.readlines()[]print stdout.readlines()[‘ 21:35:05 up 1 day, 55 min,  2 users,  load average: 0.00, 0.00, 0.00\n‘]


相當於shell當中的 

ssh -p58422 [email protected] -o StrictHostKeyChecking=no ‘uptime‘


2 subprocess(盡量不要用這個模組,返回不美觀,如果只是執行shell命令推薦commands模組)

python內建模組

參考:http://www.jb51.net/article/48086.htm

常用fork子進程執行shell命令,可以返回結果和傳回值

舉例:
只需要傳回值

In [6]: retcode = subprocess.call(‘ls -l‘, shell=True)total 12-rw-rw-r--. 1 oldboy oldboy 239 Jan 19 21:13 access.log-rw-rw-r--. 1 oldboy oldboy 458 Jan 19 20:50 arp.txt-rw-r--r--. 1 oldboy oldboy 184 Jan 16 12:04 hostsIn [7]: print retcode0


注意: 

shell預設為False,等於 retcode = subprocess.call(["ls", "-l"])  列表的形式第一個為命令,後面的都作為參數傳遞


需要傳回值

child1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE)child1.stdout.readlines()


3 comands模組

python內建模組

 status,output = commands.getstatusoutput(‘cat /etc/passwd‘)

優點: 無論命令執行錯誤與正確,正確輸出和錯誤輸出都以字串原樣的字串形式傳遞給output



4 shutil模組

python內建模組

參考:http://www.jb51.net/article/50070.htm

作用:檔案的拷貝,因為os.模組中沒有拷貝檔案或者目錄的操作,所以shutil這個模組來取代這個短處

shutil.copyfile( src, dst) 從源src複製到dst中去。當然前提是目標地址是具備可寫入權限。拋出的異常資訊為IOException. 如果當前的dst已存在的話就會被覆蓋掉
shutil.copyfile(‘/etc/passwd‘,‘/tmp‘)
shutil.copytree(‘/tmp/d1/‘,‘/tmp/d2‘)



本文出自 “崔德華營運打工從業路” 部落格,請務必保留此出處http://cuidehua.blog.51cto.com/5449828/1761922

python中常用到的模組和包名稱

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.