1. 程式人生 > >Python學習之旅(二十九)

Python學習之旅(二十九)

Python基礎知識(28):常用第三方模組

一、Pillow

PIL(Python Imaging Library):提供了強大的影象操作功能,可以通過簡單的程式碼完成複雜的影象處理,是Python平臺事實上的影象處理庫,支援Python 2.7以及更低的版本

Pillow:在PIL基礎上建立的相容版本,同時加入了更多新特性,支援Python 3.x

1、安裝Pillow

如果安裝了Anaconda,Pillow就已經可用了。否則,需要在命令列下通過pip安裝

2、操作影象

(1)影象縮放

from PIL import Image

#在當前路徑下,開啟一個jpg影象檔案
im = Image.open('test.jpg') #獲得影象尺寸 w, h = im.size print('Original image size: %sx%s' % (w, h)) #縮放50% im.thumbnail((w//2, h//2)) print('Resize image to: %sx%s' % (w//2, h//2)) #把縮放後的影象用jpeg格式儲存 im.save('thumbnail.jpg', 'jpeg') 結果: Original image size: 1000x1000 Resize image to: 500x500

(2)模糊效果

from PIL import Image, ImageFilter

# 開啟一個jpg影象檔案,注意是當前路徑:
im = Image.open('test.jpg')
# 應用模糊濾鏡:
im2 = im.filter(ImageFilter.GaussianBlur)
im2.save('blur.jpg', 'jpeg')

效果如下:

 

(3)生成字母驗證碼

from PIL import Image, ImageDraw, ImageFont, ImageFilter
import os
import random
#隨機字母
def rndChar():
    return chr(random.randint(65, 90))
#隨機顏色1:
def rndColor():
    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
#隨機顏色2:
def rndColor2():
    
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) #240 x 60: width = 60 * 4 height = 60 image = Image.new('RGB', (width, height), (255,255,255)) #建立Font物件 #font = ImageFont.truetype('Arial.ttf', 36) font = ImageFont.truetype('arial.ttf', 36) #建立Draw物件: draw = ImageDraw.Draw(image) #填充每個畫素: for x in range(width): for y in range(height): draw.point((x, y), fill=rndColor()) #輸出文字: for t in range(4): draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2()) #模糊: image = image.filter(ImageFilter.BLUR) image.save('code.jpg', 'jpeg')

驗證碼:

二、requests

用於訪問網路資源,處理URL資源時比urllib模組更方便

1、安裝requests

如果安裝了Anaconda,requests就已經可用了。否則,需要在命令列下通過pip安裝

2、使用requests

(1)通過GET訪問一個頁面

>>> import requests
>>> r = requests.get('https://www.douban.com/') # 豆瓣首頁
>>> r.status_code
200
>>> r.text
r.text
'<!DOCTYPE HTML>\n<html>\n<head>\n<meta name="description" content="提供圖書、電影、音樂唱片的推薦、評論和...'

(2)傳送POST請求

傳送POST請求,只需要把get()方法變成post(),然後傳入data引數作為POST請求的資料

r = requests.post('https://accounts.douban.com/login', data={'form_email': '[email protected]', 'form_password': '123456'})

三、chardet

檢測編碼,處理不規範的第三方網頁時,在不知道編碼的情況下可以用chardet,獲取編碼後在轉為str,方便後續處理

(1)安裝chardet

如果安裝了Anaconda,chardet就已經可用了。否則,需要在命令列下通過pip安裝

(2)使用chardet

用chardet檢測編碼

import chardet
s = chardet.detect(b'Hello,world!')
print(s)

結果:
{'encoding': 'ascii', 'confidence': 1.0, 'language': ''}
檢測出的編碼是ascii,注意到還有個confidence欄位,表示檢測的概率是1.0(即100%)

檢測GBK編碼的中文

import chardet
data = '人生苦短,我學Python'.encode('gbk')
s = chardet.detect(data)
print(s)

結果:
{'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}

對日文進行檢測

import chardet
#新的一天開始了,要好好加油哦
data = '新しい一日が始まったから、頑張ってね'.encode('euc-jp')
s = chardet.detect(data)
print(s)

結果:
{'encoding': 'EUC-JP', 'confidence': 1.0, 'language': 'Japanese'}

ardet支援檢測中文、日文、韓文等多種語言

四、psutil

在Python中獲取系統資訊,可以跨平臺使用,支援Linux/UNIX/OSX/Windows等,是系統管理員運維人員不可或缺的必備模組

(1)安裝psutil

如果安裝了Anaconda,psutil就已經可用了。否則,需要在命令列下通過pip安裝

(2)獲取CPU資訊

獲取CPU資訊

import psutil
a = psutil.cpu_count()#CPU邏輯數量
b = psutil.cpu_count(logical=False)#CPU物理核心
print(a)
print(b)

結果:
4
4

統計CPU的使用者/系統/空閒時間

import psutil
a = psutil.cpu_times()
print(a)

結果:
scputimes(user=41360.28125, system=27004.4375, idle=268042.21875, interrupt=656.4062576293945, dpc=835.3125014305115)

再實現類似top命令的CPU使用率,每秒重新整理一次,累計10次

import psutil
for x in range(10):
    a = psutil.cpu_percent(interval=1, percpu=True)
    print(a)

結果:
[40.6, 28.1, 31.2, 24.6]
[46.9, 32.8, 42.2, 43.8]
[31.8, 31.2, 28.1, 28.1]
[58.2, 56.9, 57.8, 56.2]
[38.2, 40.6, 43.8, 34.9]
[44.6, 51.6, 46.9, 40.9]
[34.3, 32.3, 34.4, 25.0]
[34.3, 32.3, 32.8, 17.2]
[39.4, 42.2, 32.8, 34.4]
[57.6, 67.2, 64.1, 54.7

(3)獲取記憶體資訊

使用psutil獲取實體記憶體和交換記憶體資訊

import psutil
a = psutil.virtual_memory()
b = psutil.swap_memory()
print(a)
print(b)

結果:
svmem(total=3679428608, available=730165248, percent=80.2, used=2949263360, free=730165248)
sswap(total=6778257408, used=5174353920, free=1603903488, percent=76.3, sin=0, sout=0)

(4)獲取磁碟資訊

通過psutil獲取磁碟分割槽、磁碟使用率和磁碟IO資訊

import psutil
#磁碟分割槽資訊
a = psutil.disk_partitions()
#磁碟使用情況
b = psutil.disk_usage('/')
#磁碟IO
c = psutil.disk_io_counters()
print(a)
print(b)
print(c)

結果:
[sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed')]
sdiskusage(total=378581020672, used=107236286464, free=271344734208, percent=28.3)
sdiskio(read_count=1813882, write_count=639951, read_bytes=59520503808, write_bytes=27710433792, read_time=58003, write_time=10482)

(5)獲取網路資訊

psutil可以獲取網路介面和網路連線資訊

import psutil
#獲取網路讀寫位元組/包的個數
a = psutil.net_io_counters()
#獲取網路介面資訊
b = psutil.net_if_addrs()
#獲取網路介面狀態
c = psutil.net_if_stats()
print(a)
print(b)
print(c)

結果:
snetio(bytes_sent=118923812, bytes_recv=1100353188, packets_sent=513738, packets_recv=122094, errin=0, errout=0, dropin=0, dropout=0)
{'WLAN': [snic(family=<AddressFamily.AF_LINK: -1>, address='74-DF-BF-5E-C3-29', netmask=None, broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET: 2>, address='169.254.55.6', netmask='255.255.0.0', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 23>, address='fe80::b122:21:c891:3706', netmask=None, broadcast=None, ptp=None)], '本地連線* 3': [snic(family=<AddressFamily.AF_LINK: -1>, address='76-DF-BF-5E-C3-29', netmask=None, broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET: 2>, address='169.254.253.30', netmask='255.255.0.0', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 23>, address='fe80::d064:e706:8cee:fd1e', netmask=None, broadcast=None, ptp=None)], '本地連線* 4': [snic(family=<AddressFamily.AF_LINK: -1>, address='86-DF-BF-5E-C3-29', netmask=None, broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET: 2>, address='169.254.231.149', netmask='255.255.0.0', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 23>, address='fe80::6d45:5ace:6385:e795', netmask=None, broadcast=None, ptp=None)], '乙太網': [snic(family=<AddressFamily.AF_LINK: -1>, address='54-EE-75-A4-F6-66', netmask=None, broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET: 2>, address='192.168.96.168', netmask='255.255.255.0', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 23>, address='fe80::94c9:c414:29e2:897', netmask=None, broadcast=None, ptp=None)], 'VMware Network Adapter VMnet1': [snic(family=<AddressFamily.AF_LINK: -1>, address='00-50-56-C0-00-01', netmask=None, broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET: 2>, address='192.168.15.1', netmask='255.255.255.0', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 23>, address='fe80::d07c:26c3:1b0:674d', netmask=None, broadcast=None, ptp=None)], 'VMware Network Adapter VMnet2': [snic(family=<AddressFamily.AF_LINK: -1>, address='00-50-56-C0-00-02', netmask=None, broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET: 2>, address='192.168.2.1', netmask='255.255.255.0', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 23>, address='fe80::55fc:e8e8:e0f4:54ed', netmask=None, broadcast=None, ptp=None)], 'Loopback Pseudo-Interface 1': [snic(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None), snic(family=<AddressFamily.AF_INET6: 23>, address='::1', netmask=None, broadcast=None, ptp=None)]}
{'乙太網': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500), 'VMware Network Adapter VMnet1': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500), 'VMware Network Adapter VMnet2': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500), 'Loopback Pseudo-Interface 1': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=1073, mtu=1500), 'WLAN': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500), '本地連線* 3': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500), '本地連線* 4': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=0, mtu=1500)}

獲取當前網路連線資訊

import psutil
a = psutil.net_connections()
print(a)

結果:
[sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=2, laddr=addr(ip='::', port=500), raddr=(), status='NONE', pid=1740), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=443), raddr=(), status='LISTEN', pid=4668), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=59978), raddr=addr(ip='127.0.0.1', port=59977), status='ESTABLISHED', pid=11376), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=59980), raddr=addr(ip='127.0.0.1', port=59979), status='ESTABLISHED', pid=11376), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='192.168.15.1', port=2177), raddr=(), status='NONE', pid=7540), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=49676), raddr=(), status='LISTEN', pid=868), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=64843), raddr=(), status='NONE', pid=3184), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='192.168.15.1', port=138), raddr=(), status='NONE', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=62610), raddr=addr(ip='23.217.125.240', port=80), status='TIME_WAIT', pid=0), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=6682), raddr=(), status='NONE', pid=2836), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=64831), raddr=(), status='NONE', pid=6916), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=60003), raddr=(), status='LISTEN', pid=11376), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=59979), raddr=addr(ip='127.0.0.1', port=59980), status='ESTABLISHED', pid=11376), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=2, laddr=addr(ip='fe80::94c9:c414:29e2:897', port=2177), raddr=(), status='NONE', pid=7540), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=445), raddr=(), status='LISTEN', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=2, laddr=addr(ip='fe80::55fc:e8e8:e0f4:54ed', port=2177), raddr=(), status='NONE', pid=7540), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=6942), raddr=(), status='LISTEN', pid=11376), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::', port=49665), raddr=(), status='LISTEN', pid=1260), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=2, laddr=addr(ip='::', port=5355), raddr=(), status='NONE', pid=1924), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=135), raddr=(), status='LISTEN', pid=1080), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=56480), raddr=(), status='NONE', pid=2836), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='192.168.96.168', port=138), raddr=(), status='NONE', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::1', port=8307), raddr=(), status='LISTEN', pid=4668), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.15.1', port=139), raddr=(), status='LISTEN', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=80), raddr=(), status='LISTEN', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=6666), raddr=(), status='NONE', pid=2836), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='192.168.96.168', port=2177), raddr=(), status='NONE', pid=7540), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::', port=49676), raddr=(), status='LISTEN', pid=868), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=59977), raddr=addr(ip='127.0.0.1', port=59978), status='ESTABLISHED', pid=11376), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::', port=443), raddr=(), status='LISTEN', pid=4668), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=62634), raddr=addr(ip='223.202.62.231', port=443), status='SYN_SENT', pid=4740), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=49665), raddr=(), status='LISTEN', pid=1260), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=56309), raddr=addr(ip='113.96.232.146', port=8080), status='ESTABLISHED', pid=11164), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=2, laddr=addr(ip='::', port=4500), raddr=(), status='NONE', pid=1740), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=60281), raddr=addr(ip='127.0.0.1', port=60280), status='ESTABLISHED', pid=4668), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=63342), raddr=(), status='LISTEN', pid=11376), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=2, laddr=addr(ip='fe80::d07c:26c3:1b0:674d', port=2177), raddr=(), status='NONE', pid=7540), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=28653), raddr=(), status='LISTEN', pid=11164), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=49664), raddr=(), status='LISTEN', pid=736), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=912), raddr=(), status='LISTEN', pid=3204), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=59214), raddr=(), status='NONE', pid=11164), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=59005), raddr=(), status='NONE', pid=2784), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::', port=445), raddr=(), status='LISTEN', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::', port=135), raddr=(), status='LISTEN', pid=1080), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=5050), raddr=(), status='NONE', pid=1652), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=8307), raddr=(), status='LISTEN', pid=4668), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=21440), raddr=(), status='LISTEN', pid=12916), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='192.168.96.168', port=137), raddr=(), status='NONE', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=49667), raddr=(), status='LISTEN', pid=2384), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=62616), raddr=addr(ip='120.55.34.37', port=443), status='TIME_WAIT', pid=0), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=139), raddr=(), status='LISTEN', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=56248), raddr=addr(ip='1.192.193.180', port=80), status='ESTABLISHED', pid=6916), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=62632), raddr=addr(ip='59.37.97.23', port=443), status='ESTABLISHED', pid=11164), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=55965), raddr=addr(ip='113.96.208.198', port=8080), status='ESTABLISHED', pid=11352), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='192.168.2.1', port=137), raddr=(), status='NONE', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=5355), raddr=(), status='NONE', pid=1924), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=49668), raddr=(), status='LISTEN', pid=896), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=5040), raddr=(), status='LISTEN', pid=1652), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='192.168.15.1', port=137), raddr=(), status='NONE', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::', port=80), raddr=(), status='LISTEN', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=60280), raddr=addr(ip='127.0.0.1', port=60281), status='ESTABLISHED', pid=4668), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::', port=49667), raddr=(), status='LISTEN', pid=2384), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='127.0.0.1', port=59018), raddr=(), status='NONE', pid=1740), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=4500), raddr=(), status='NONE', pid=1740), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=64766), raddr=(), status='NONE', pid=2784), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=3600), raddr=(), status='NONE', pid=6916), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.2.1', port=139), raddr=(), status='LISTEN', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=2, laddr=addr(ip='::', port=5353), raddr=(), status='NONE', pid=1924), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::', port=49668), raddr=(), status='LISTEN', pid=896), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=500), raddr=(), status='NONE', pid=1740), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='192.168.2.1', port=2177), raddr=(), status='NONE', pid=7540), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=56353), raddr=addr(ip='180.163.238.166', port=80), status='ESTABLISHED', pid=6916), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=53088), raddr=(), status='NONE', pid=2836), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=56004), raddr=addr(ip='52.230.80.159', port=443), status='ESTABLISHED', pid=1740), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='192.168.2.1', port=138), raddr=(), status='NONE', pid=4), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=56051), raddr=addr(ip='104.78.72.7', port=443), status='CLOSE_WAIT', pid=2800), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::', port=49664), raddr=(), status='LISTEN', pid=736), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=21441), raddr=(), status='LISTEN', pid=12916), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='0.0.0.0', port=5353), raddr=(), status='NONE', pid=1924), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=62623), raddr=addr(ip='120.55.34.37', port=443), status='TIME_WAIT', pid=0), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=2, laddr=addr(ip='127.0.0.1', port=59213), raddr=(), status='NONE', pid=11164), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=902), raddr=(), status='LISTEN', pid=3204), sconn(fd=-1, family=<AddressFamily.AF_INET6: 23>, type=1, laddr=addr(ip='::', port=49666), raddr=(), status='LISTEN', pid=1740), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='0.0.0.0', port=49666), raddr=(), status='LISTEN', pid=1740), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='127.0.0.1', port=31752), raddr=(), status='LISTEN', pid=4740), sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=1, laddr=addr(ip='192.168.96.168', port=62630), raddr=addr(ip='14.17.42.43', port=36688), status='TIME_WAIT', pid=0)]

(6)獲取程序資訊

通過psutil可以獲取到所有程序的詳細資訊

>>> psutil.pids() # 所有程序ID
[3865, 3864, 3863, 3856, 3855, 3853, 3776, ..., 45, 44, 1, 0]
>>> p = psutil.Process(3776) # 獲取指定程序ID=3776,其實就是當前Python互動環境
>>> p.name() # 程序名稱
'python3.6'
>>> p.exe() # 程序exe路徑
'/Users/michael/anaconda3/bin/python3.6'
>>> p.cwd() # 程序工作目錄
'/Users/michael'
>>> p.cmdline() # 程序啟動的命令列
['python3']
>>> p.ppid() # 父程序ID
3765
>>> p.parent() # 父程序
<psutil.Process(pid=3765, name='bash') at 4503144040>
>>> p.children() # 子程序列表
[]
>>> p.status() # 程序狀態
'running'
>>> p.username() # 程序使用者名稱
'michael'
>>> p.create_time() # 程序建立時間
1511052731.120333
>>> p.terminal() # 程序終端
'/dev/ttys002'
>>> p.cpu_times() # 程序使用的CPU時間
pcputimes(user=0.081150144, system=0.053269812, children_user=0.0, children_system=0.0)
>>> p.memory_info() # 程序使用的記憶體
pmem(rss=8310784, vms=2481725440, pfaults=3207, pageins=18)
>>> p.open_files() # 程序開啟的檔案
[]
>>> p.connections() # 程序相關網路連線
[]
>>> p.num_threads() # 程序的執行緒數量
1
>>> p.threads() # 所有執行緒資訊
[pthread(id=1, user_time=0.090318, system_time=0.062736)]
>>> p.environ() # 程序環境變數
{'SHELL': '/bin/bash', 'PATH': '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:...', 'PWD': '/Users/michael', 'LANG': 'zh_CN.UTF-8', ...}
>>> p.terminate() # 結束程序
Terminated: 15 <-- 自己把自己結束了