系统综合实践第五次作业

Python环境搭建

目录

配置文件

  • 所设的工作路径等等需要挂载需要运行的py文件
  • requirement.txt是python项目可导出的该项目所需依赖包
  • RUN去安装requirement.txt内记录的所需依赖包,build时装到镜像中
  • CMD在启动容器时,参数为空执行默认hello.py文件

Dockerfile

 #DockerfileFROM python:3

 #工作路径
WORKDIR /usr/src/app    

 #拷贝文件
COPY requirements.txt ./    

 #根据requirement.txt安装项目所需包
RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.douban.com/simple 

 #指定容器启动时默认要运行的程序
CMD [ "python", "./hello.py" ]

requirments.txt

#requirement.txt
PyMySQL
opencv-python

生成镜像


项目构建

1、简单hello.py

print("hello world!")

  • 参数:--rm 容器退出即删除  --name设置容器名  -v挂载  默认参数(无python 文件.py)

2、简单日历输出

import calendar
yy = int(input("输入年份: "))
mm = int(input("输入月份: "))
print(calendar.month(yy,mm))

  • 参数没变

3、链接数据库

import pymysql
class Mysql():
    def __init__(self,ipaddress,urname,pd,db_name,table_name):
        self.ip=ipaddress
        self.username=urname
        self.password=pd
        self.db_name=db_name
        self.table_name=table_name
    def connect(self):
        self.conn=pymysql.connect(self.ip,self.username,self.password,self.db_name)
        if(self.conn!=None):
                print("yes")
        self.cursor=self.conn.cursor()
    def createtable(self,sql):
        self.cursor.execute('drop table if exists %s;' %self.table_name)
        self.cursor.execute(sql)
    def insertdata(self,sql):
        try:
            self.cursor.execute(sql)
            self.conn.commit()
        except :
            self.conn.rollback()
    def selectall(self):
        sql='select * from %s' %self.table_name
        self.cursor.execute(sql)
        return self.cursor.fetchall()
    def updatedata(self):
        try:
            self.cursor.execute(sql)
            self.conn.commit()

        except :
            self.conn.rollback()        
    def deletedata(self):
        try:
            self.cursor.execute(sql)
            self.conn.commit()
        except :
            self.conn.rollback()
    def close(self):
        self.conn.close()

ip='lnmp_mysql-container'          #容器名
username='root'         #用户名
password='123'      #密码
db_name='myDB' #数据库名
table_name='Stu'    #表名
db=Mysql(ip, username, password, db_name,table_name)
db.connect()

sql="""create table Stu (
       id varchar(10) primary key,
       name varchar(20)
        );"""
db.createtable(sql)
sql1="insert into Stu values(123,'abc');" 
sql2="insert into Stu values(456,'bcd');" 
db.insertdata(sql1)
db.insertdata(sql2)
print(db.selectall())

db.close()
  • 数据库连接上次lnmp实验留下的容器
  • 指令需要设置连接和相应网络,ps:这里需要docker inspect 容器名/id去查看Networking Bridge
  • 参数:--link 容器名  --net加入容器对应网桥

进入数据库查看:

4、opencv

import cv2 as cv

img1 = cv.imread("photo1.png")
img2 = cv.imread("photo2.png")

result = cv.addWeighted(img1,0.3,img2,0.3,0)  
cv.imwrite('re.png', result)
print("success!")

实验总结

  • 大致时间4-5小时
  • 大致较为顺利,就是在使用openvc的时候查找和使用相关API遇到了一点困难
posted @ 2020-05-22 23:38  Shen_HX  阅读(138)  评论(0编辑  收藏  举报