如何用Python获取系统内核及磁盘详细信息?
- 内容介绍
- 文章标签
- 相关推荐
本文共计382个文字,预计阅读时间需要2分钟。
在MySQL 8中,Python可以通过多种方式与数据库交互。以下是一个简单的示例,展示了如何使用Python的`mysql-connector-python`库来连接MySQL数据库并执行查询:
pythonimport mysql.connector
连接数据库conn=mysql.connector.connect( host='localhost', # 数据库主机地址 user='root', # 数据库用户名 password='your_password', # 数据库密码 database='your_database' # 数据库名)
创建游标对象cursor=conn.cursor()
执行查询cursor.execute(SELECT * FROM your_table)
获取查询结果results=cursor.fetchall()
输出结果for row in results: print(row)
关闭游标和连接cursor.close()conn.close()
#!/usr/bin/env python
import subprocess
def uname_func():
uname = "uname"
uname_arg = "-a"
print "Gathing system information with %s command:/n" % uname
subprocess.call([uname,uname_arg])
def disk_func():
diskspace = "df"
diskspace_arg = "-h"
print "Gathering diskspace information %s command:/n" % diskspace
subprocess.call([diskspace, diskspace_arg])
def main():
uname_func()
disk_func()
if __name__ == "__main__":
main()[root@mysql8 python]# python getLinuxInfo.py
Gathing system information with uname command:
Linux mysql8 3.10.0-1160.el7.x86_64 #1 SMP Tue Aug 18 14:50:17 EDT 2020 x86_64 x86_64 x86_64 GNU/Linux
Gathering diskspace information df command:
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 8.7M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 91G 6.5G 85G 8% /
/dev/sda1 1014M 140M 875M 14% /boot
tmpfs 799M 0 799M 0% /run/user/0
版权声明:本文为博主原创文章,未经博主允许不得转载。
Linux,oracle
本文共计382个文字,预计阅读时间需要2分钟。
在MySQL 8中,Python可以通过多种方式与数据库交互。以下是一个简单的示例,展示了如何使用Python的`mysql-connector-python`库来连接MySQL数据库并执行查询:
pythonimport mysql.connector
连接数据库conn=mysql.connector.connect( host='localhost', # 数据库主机地址 user='root', # 数据库用户名 password='your_password', # 数据库密码 database='your_database' # 数据库名)
创建游标对象cursor=conn.cursor()
执行查询cursor.execute(SELECT * FROM your_table)
获取查询结果results=cursor.fetchall()
输出结果for row in results: print(row)
关闭游标和连接cursor.close()conn.close()
#!/usr/bin/env python
import subprocess
def uname_func():
uname = "uname"
uname_arg = "-a"
print "Gathing system information with %s command:/n" % uname
subprocess.call([uname,uname_arg])
def disk_func():
diskspace = "df"
diskspace_arg = "-h"
print "Gathering diskspace information %s command:/n" % diskspace
subprocess.call([diskspace, diskspace_arg])
def main():
uname_func()
disk_func()
if __name__ == "__main__":
main()[root@mysql8 python]# python getLinuxInfo.py
Gathing system information with uname command:
Linux mysql8 3.10.0-1160.el7.x86_64 #1 SMP Tue Aug 18 14:50:17 EDT 2020 x86_64 x86_64 x86_64 GNU/Linux
Gathering diskspace information df command:
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 8.7M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 91G 6.5G 85G 8% /
/dev/sda1 1014M 140M 875M 14% /boot
tmpfs 799M 0 799M 0% /run/user/0
版权声明:本文为博主原创文章,未经博主允许不得转载。
Linux,oracle

