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

    一.python镜像布置

    1.树形结构

    hello.py--python容器的测试代码
    date.py--日历代码
    op.py--opencv程序代码
    date.py--数据库代码
    test.jpg--测试图片
    test-rotated--opencv运行结果图片
    

    2.dockerfile

    FROM python:3
    MAINTAINER monster<1263199084@qq.com>
    WORKDIR /app
    COPY ./requirements.txt /requirements.txt
    RUN pip install -r /requirements.txt -i https://pypi.douban.com/simple  #修改源并安装依赖
    ENTRYPOINT ["python"]
    CMD ["hello.py"] #默认打开文件
    

    3.requirements

    PyMySQL
    opencv-python
    

    二.Hello World

    -v将本地目录挂载到python工作目录以方便代码修改

    --rm运行完毕后自动移除容器

    vim hello.py
    print('hello world')
    
    sudo docker run -it --rm -v /home/monster/python/app:/app python:test1 hello.py
    

    三.日历输出(参考菜鸟教程)

    vim date.py
    import calendar
     
    # 输入指定年月
    yy = int(input("输入年份: "))
    mm = int(input("输入月份: "))
     
    # 显示日历
    print(calendar.month(yy,mm))
    

    交互式终端需要 -it 指令

    sudo docker run -it --rm -v /home/monster/python/app:/app python:test1 date.py
    

    四.mysql数据库操作(使用实践二中的数据库

    import pymysql
    
    # 打开数据库连接
    db = pymysql.connect("mysql", "docker", "123456", "docker_mysql")
    #创建游标对象
    
    cursor = db.cursor()
    #先查询一次数据库数据
    sql = """select * FROM test"""
    cursor.execute(sql)
    results = cursor.fetchall()
    print(results)
    
    #SQL插入语句
    sql="""insert test(id,name)
    	values(2022,'DEF')"""
    cursor.execute(sql)
    db.commit()
    
    #插入完成后再读取一次数据库数据
    sql = """select * FROM test"""
    cursor.execute(sql)
    results = cursor.fetchall()
    print(results)
    
    # 关闭数据库连接
    db.close()
    
    sudo docker run -it --rm -v /home/monster/python/app:/app --link=mysql:mysql python:test1  db.py
    

    未执行py文件前的docker_mysql数据库中test表

    执行py文件后的docker_mysql数据库中test表

    五.opencv程序

    #做一个图片翻转功能
    import cv2
    img=cv2.imread('test.jpg',flags=1)
    rows,cols=img.shape[:2]
    M=cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
    dst=cv2.warpAffine(img,M,(cols,rows))
    cv2.imwrite("test-rotated.jpg", dst, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
    print('rotated and saved.')
    
     sudo docker run -it --rm -v /home/monster/python/app:/app  python:test1 op.py
    

    六.实验总结

    前后花费大概4小时的时间吧,和实践四相比起来,轻松多了

  • 相关阅读:
    Keil(MDK) 5 软件安装教程
    JPA 的specification动态查询
    idea 建立JPA项目(二)
    HBase单节点的安装与配置
    【codevs3012+codevs3037】线段覆盖4+线段覆盖5(DP)
    MySQL下载和安装教程
    二、单线程的 JavaScript
    #JS 基础之异步(一)
    JS 基础之: 继承的 六 种实现方式
    源码浅析-Vue3中的13个全局Api
  • 原文地址:https://www.cnblogs.com/ljw1999/p/12906193.html
Copyright © 2011-2022 走看看