一、回顾
[root@python ~]# vim test.py a=3 b=4 print(a+b) print(a**2+b**2) print(25**0.5) [root@python ~]# python3 test.py 7 25 5.0 #调试test.py脚本 [root@python ~]# python3 -m pdb test.py > /root/test.py(1)<module>() -> a=3 (Pdb) n > /root/test.py(2)<module>() -> b=4 (Pdb) n > /root/test.py(3)<module>() -> print(a+b) (Pdb) n 7 > /root/test.py(4)<module>() -> print(a**2+b**2) (Pdb) n 25 > /root/test.py(5)<module>() -> print(25**0.5) (Pdb) n 5.0 --Return-- > /root/test.py(5)<module>()->None -> print(25**0.5) [root@python ~]# python3 Python 3.6.8 (default, Nov 14 2023, 16:29:52) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import random >>> random <module 'random' from '/usr/lib64/python3.6/random.py'> >>> [1]+ 已停止 python3 [root@python ~]# vim /usr/lib64/python3.6/random.py [root@python ~]# fg python3 >>> random <module 'random' from '/usr/lib64/python3.6/random.py'> >>> random.randint(0,10) 9 >>> random.randint(0,10) 2 >>> random.randint(0,10) 8 >>> random.randint(0,10) 4
二、python管理
1.方法一
1.设置清华镜像(从国内下载安装包,提高下载和安装速度)
2.安装pandas数据分析工具(pandas是知名的数据分析工具,pandas有完整的数据读取工具,以及DateFrame数据框架,用于保存从数据库中读取的数据 )
3.安装pymysql连接器(oracle为开发者提供的python管理mysql 的⼯具,通过这个⼯具,就恶意在不替原有代码的情况下,应对 数据库软件的升级)
[root@python ~]# pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple [root@python ~]# pip3 install pymysql [root@python ~]# pip3 install pandas
>>> import pandas >>> import pymysql >>> import pandas >>> import whel Traceback (most recent call last):File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'whel' >>> pymysql <module 'pymysql' from '/usr/local/lib64/python3.6/site-packages/pymysql/__init__.py'> >>> pandas <module 'pandas' from '/usr/local/lib64/python3.6/site-packages/pandas/__init__.py'> >>> import pymysql as pm >>> import pandas as pd >>> pm <module 'pymysql' from '/usr/local/lib64/python3.6/site-packages/pymysql/__init__.py'> >>> pd <module 'pandas' from '/usr/local/lib64/python3.6/site-packages/pandas/__init__.py'> >>> conn=pm.connect( ... host='123.249.27.70', ... user='abcd', ... password='abcd', ... database='test', ... port=6001); >>> conn <pymysql.connections.Connection object at 0x7fc060751400> >>> cursor=conn.cursor() >>> cursor <pymysql.cursors.Cursor object at 0x7fc060e87860> >>> sql="select * from student" >>> sql 'select * from student' >>> cursor.execute(sql) 5 >>> cursor.fetchall()File "<stdin>", line 1cursor.fetchall()^ IndentationError: unexpected indent >>> cursor.fetchall() ((1, '章三', '男'), (2, '李四', '女'), (3, '小凤仙', '女'), (4, '章丘铁锅', '男'), (6, '孙颖莎', '女')) >>> cursor.description (('id', 3, None, 11, 11, 0, False), ('name', 253, None, 180, 180, 0, False), ('gender', 253, None, 16, 16, 0, False)) >>> head=[] >>> desc=cursor.description >>> for var in desc: ... print(var[0]) ... id name gender >>> for var in desc: ... head.append(var[0]) ... >>> head ['id', 'name', 'gender'] >>> pd.DataFrame(data=cursor.fetchall(),columns=head) Empty DataFrame Columns: [id, name, gender] Index: [] >>> cursor.fetchall() () >>> cursor.execute(sql) 5 >>> res=cursor.fetchall() >>> res ((1, '章三', '男'), (2, '李四', '女'), (3, '小凤仙', '女'), (4, '章丘铁锅', '男'), (6, '孙颖莎', '女')) >>> head ['id', 'name', 'gender'] >>> pd.DataFrame(data=res,columns=head)id name gender 0 1 章三 男 1 2 李四 女 2 3 小凤仙 女 3 4 章丘铁锅 男 4 6 孙颖莎 女 >>>
2.方法二
[root@python ~]# vim python_mysql.py class Python_Mysql_01(object): def __init__(self):print("test") if __name__=="__main__":pmp1=Python_Mysql_01()[root@python ~]# python3 python_mysql.py test
import pymysql class Python_Mysql_01(object): def __init__(self):print("test")def getConn(self):conn=pymysql.connect(host='123.249.27.70',user='abcd',password='abcd',database='test',port=6001)print(conn)return conn if __name__=="__main__":#初始化Python_Mysql_01类,创建实例,pmp,之后所有的方法都可以在实列中调用pmp=Python_Mysql_01()pmp.getConn()[root@python ~]# python3 python_mysql.py test <pymysql.connections.Connection object at 0x7f9952e396a0>
import pymysql class Python_Mysql_01(object): def __init__(self):print("test")def getConn(self):conn=pymysql.connect(host='123.249.27.70',user='abcd',password='abcd',database='test',port=6001) # print(conn)return conn if __name__=="__main__":#初始化Python_Mysql_01类,创建实例,pmp,之后所有的方法都可以在实列中调用pmp=Python_Mysql_01()# 获得connconn=pmp.getConn()#获得游标cursor=conn.cursor()print(cursor) [root@python ~]# python3 python_mysql.py test <pymysql.cursors.Cursor object at 0x7f99af8e0d30>
[root@python ~]# vim python_mysql_01 import pymysql import pandas class Python_Mysql_01(object): def __init__(self):print("======================") def getConn(self):conn=pymysql.connect(host=input("sign host_ip|name:"),user=input("sign database username:"),password=input("sign database password:"),database=input("sign database name:"),port=int(input("sign port no "))) # print(conn)return conn def getRes(self,cursor,sql):cursor.execute(sql) # 获得查询的数据data=cursor.fetchall() # 表头head=[item[0] for item in cursor.description]# 组成pandas数据框 DataFramereturn pandas.DataFrame(data=data,columns=head) if __name__=="__main__":# 初始化Python_Mysql_01类,创建实例,pmp,之后所有的方法都可以在实例中调用pmp=Python_Mysql_01()# 获得connconn=pmp.getConn() #获得游标 cursor=conn.cursor() # print(cursor)tablename=input("sign tablename")df=pmp.getRes(cursor,"select * from "+tablename)print(df)
[root@python ~]# python3 -m pip install --upgrade pip [root@python ~]# which pyinstaller /usr/local/bin/pyinstaller [root@python ~]# pyinstaller --onefile python_mysql_01.py [root@python ~]# ls anaconda-ks.cfg dist py001.py py003.py python_mysql_01.py python_mysql.py build for.py py002.py __pycache__ python_mysql_01.spec test.py [root@python ~]# cd dist/ [root@python dist]# ls python_mysql_01 [root@python dist]# ./python_mysql_01 sign host_ip|name:123.249.27.70 sign database username:abcd sign database password:abcd sign database name:test sign port no 6001 sign tablenamestudentid name gender 0 1 章三 男 1 2 李四 女 2 3 小凤仙 女 3 4 章丘铁锅 男 4 6 孙颖莎 女
[root@python dist]# cd [root@python ~]# python3 -m http.server 9000 Serving HTTP on 0.0.0.0 port 9000 (http://0.0.0.0:9000/) ... 192.168.2.1 - - [09/Aug/2024 17:36:42] "GET / HTTP/1.1" 200 - 192.168.2.1 - - [09/Aug/2024 17:36:42] code 404, message File not found 192.168.2.1 - - [09/Aug/2024 17:36:42] "GET /favicon.ico HTTP/1.1" 404 -
浏览器访问:192.168.2.40:9000
后台运行 [root@python ~]# nohup python3 -m http.server 9000 & [3] 2740 [root@python ~]# nohup: 忽略输入并把输出追加到"nohup.out"