欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python学习之旅(二十九)

程序员文章站 2022-08-20 18:41:33
Python基础知识(28):常用第三方模块 一、Pillow PIL(Python Imaging Library):提供了强大的图像操作功能,可以通过简单的代码完成复杂的图像处理,是Python平台事实上的图像处理库,支持Python 2.7以及更低的版本 Pillow:在PIL基础上创建的兼容 ......

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')

效果如下:

Python学习之旅(二十九)

 

(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')

验证码:

Python学习之旅(二十九)

二、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 <-- 自己把自己结束了