zoukankan      html  css  js  c++  java
  • 系统综合实践第五次作业

    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遇到了一点困难
  • 相关阅读:
    模拟ajax请求爬取微博
    使用nohup+& 踩到的坑
    Python3爬虫一之(urllib库)
    在linux下安装并运行scrapyd
    创建Django项目并将其部署在腾讯云上
    python解析库之 XPath
    python3中urllib库的request模块详解
    HTTP协议详解
    线程之红绿灯
    win7 64 下安装MyGeneration 遇到的问题解决方法
  • 原文地址:https://www.cnblogs.com/ShenHXbloc/p/12940419.html
Copyright © 2011-2022 走看看