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': 'abc@example.com', '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 <-- 自己把自己结束了
来源:oschina
链接:https://my.oschina.net/u/4336274/blog/3718793