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': 'abc@example.com', 'form_password': ''})
三、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 <-- 自己把自己结束了
Python学习之旅(二十九)的更多相关文章
- Python学习之旅(十九)
Python基础知识(18):面向对象高级编程(Ⅰ) 使用__slots__:限制实例的属性,只允许实例对类添加某些属性 (1)实例可以随意添加属性 (2)某个实例绑定的方法对另一个实例不起作用 (3 ...
- python学习笔记(二十九)为什么python的多线程不能利用多核CPU
问题:为什么python的多线程不能利用多核CPU,但是咱们在写代码的时候,多线程的确是在并发,而且还比单线程快原因:因为GIL,python只有一个GIL,运行python时,就要拿到这个锁才能执行 ...
- Python学习笔记(二十九)ThreadLocal
import threading #创建全局ThreadLocal对象: local_school = threading.local() def process_student(): #获取当前线程 ...
- Python学习之旅(十二)
Python基础知识(11):高级特性 一.分片(切片) 通过索引来获取一定范围内的元素 #字符串 s="Alice" s[0:4:2] 结果: 'Ai' #列表 l=[1,2,3 ...
- Dynamic CRM 2013学习笔记(二十九)报表设计:reporting service 报表开发常见问题
在报表开发过程中,经常会遇到各种各样的问题,比如The report cannot be displayed. (rsProcessingAborted),一点有意义的提示都没有:再就是分页问题,经常 ...
- python学习之旅(十五)
Python基础知识(14):函数(Ⅴ) 一.装饰器 decorator:本质上就是函数,可以增强函数的功能. 定义起来虽然有点复杂,但使用起来非常灵活和方便 1.不修改被装饰函数的源代码 2.不修改 ...
- python学习笔记(二十八)日志模块
我们在写程序的时候经常会打一些日志来帮助我们查找问题,这次学习一下logging模块,在python里面如何操作日志.介绍一下logging模块,logging模块就是python里面用来操作日志的模 ...
- python接口自动化(二十九)--html测试报告通过邮件发出去——上(详解)
简介 前边几篇,已经教小伙伴们掌握了如何生成HTML的测试报告,那么生成测试报告,我们也不能放在那里不管了,这样即使你报告在漂亮,领导也看不到.因此如果想向领导汇报工作,不仅需要提供更直观的测试报告. ...
- 【Java学习笔记之二十九】Java中的"equals"和"=="的用法及区别
Java中的"equals"和"=="的用法及区别 在初学Java时,可能会经常碰到下面的代码: String str1 = new String(" ...
- 流畅python学习笔记:第十九章:动态属性和特性
首先来看一个json文件的读取.书中给出了一个json样例.该json文件有700多K,数据量充足,适合本章的例子.文件的具体内容可以在http://www.oreilly.com/pub/sc/os ...
随机推荐
- codevs 2033 邮票
洛谷 P2725 邮票 Stamps codevs 2033 邮票 题目链接 http://codevs.cn/problem/2033/ https://www.luogu.org/problemn ...
- Android 隐藏系统状态栏
通常的做法是这样的: private static boolean isStatusbarVisible(Activity activity) { int uiOptions = activity.g ...
- 动态绑定事件到特定dom元素上,包含新增加的
$('body').on('click', 'a.detail-data', function (e) { //动态事件绑定 为body元素下所有的a.detail-data元素添加一个事件 包括新增 ...
- pandas.Dataframe复杂条件过滤
https://stackoverflow.com/questions/11418192/pandas-complex-filter-on-rows-of-dataframe mask = df.ap ...
- python3 --- locale命名空间让程序更加安全了
[简介] 由于python-2.x 并没有locale这个层次的命名空间,所以临时变量有可能会泄漏,进而影响到了包涵它的命名空间 [看一下pyhont-2.x是怎么泄漏临时变量的] python Py ...
- Node.js 反序列化漏洞远程执行代码(CVE-2017-5941)
2.1 摘要 2.1.1 漏洞介绍 漏洞名称: Exploiting Node.js deserialization bug for Remote Code Execution 漏洞CVE id: C ...
- Linux零基础入门第四课
根据直播讲义整理的内容,从第四课开始.前三课的内容若后面有精力会一并整理进来. 文件的基本操作(上) 创建.删除.复制.移动和重命名 touch命令创建文件 语法 >$ touch file0 ...
- IDEA自动生成序列化ID
1.安装 serialVersionUID 插件: 2.设置你的IDEA为检查序列化ID 3.重启IDEA(有的版本可以不重启) 4.在需要自动生成序列化ID的类中使用快捷键:Alt+Insert 转 ...
- C# 正则表达式判断是否是数字、是否含有中文、是否是数字字母组合
//判断输入是否包含中文 不管你有没有输入英文,只要包含中文,就返回 true public static bool HasChinese(string content) { //判断是不是中文 st ...
- python 序列化模块之 json 和 pickle
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,支持不同程序之间的数据转换.但是只能转换简单的类型如:(列表.字典.字符串. ...