zoukankan      html  css  js  c++  java
  • 第5次实践作业

    一.python镜像布置

    (1)树型结构

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

    (2)dockerfile

    FROM python
    MAINTAINER p4<584979330@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.txt

    PyMySQL
    opencv-python
    

    (4)构建镜像

    sudo docker build -t python:ex5 .
    


    二、Hello World

    hello.py

    print('hello world')
    

    运行

    sudo docker run -it --rm -v /home/p4/python/app:/app python:ex5 hello.py
    

    三、日历输出

    date.py

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

    运行

    sudo docker run -it --rm -v /home/p4/python/app:/app python:ex5 date.py
    

    四、数据库操作

    这里采用的是实践二的数据库
    db.py

    import pymysql
     
    # 打开数据库连接
    db = pymysql.connect("ssmysql", "css", "888", "docker_mysql")
    
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
     
    # 使用 execute()  方法执行 SQL 查询 
    cursor.execute("SELECT VERSION()")
     
    # 使用 fetchone() 方法获取单条数据.
    data = cursor.fetchone()
     
    print ("Database version : %s " % data)
    
    # 使用 execute() 方法执行 SQL,如果表存在则删除
    cursor.execute("DROP TABLE IF EXISTS `user`")
    
    sql = """CREATE TABLE IF NOT EXISTS `user`(
             `id` bigint(20) NOT NULL,
             `name` varchar(50) DEFAULT NULL,
             `age` int DEFAULT NULL),
             PRIMARY KEY(`id`)"""
    
    cursor.execute(sql)
    
    # SQL 插入语句
    sql = """INSERT INTO `user`(`id`,
             `name`,`age`)
             VALUES (555, 'zxr',21)"""
    try:
       # 执行sql语句
       cursor.execute(sql)
       # 提交到数据库执行
       db.commit()
    except:
       # 如果发生错误则回滚
       db.rollback()
    
    # SQL 查询语句
    sql = "SELECT * FROM user"
    
    try:
       # 执行SQL语句
       cursor.execute(sql)
       # 获取所有记录列表
       results = cursor.fetchall()
       for row in results:
          id = row[0]
          name = row[1]
          age = row[2]
           # 打印结果
          print ("id=%s,name=%s,age=%s" % 
                 (id, name, age))
    except:
       print ("Error: unable to fetch data")
    
    # 关闭数据库连接
    db.close()
    

    运行

    sudo docker run -it --rm -v /home/p4/python/app:/app --link=ssmysql:ssmysql python:ex5  db.py
    

    进入数据库的容器确认

    五、opencv程序

    cv.py

    #做一个图片翻转功能
    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/p4/python/app:/app python:ex5 cv.py
    

    原图和旋转之后的图片

    六、遇到的问题及解决办法

    在运行db.py文件时出现了报错

    仔细查看发现是文件中的一个字母打错了!告诉自己,做事情要细心!

    七、花费的时间

    项目 时间
    镜像构建 0.5h
    文件编写及数据库操作 1.5h
    博客编写 1h
    总计 3h

    八、总结

    这次的实验相对于实验四来说真的是轻松很多,做的比较顺利。通过这次实验学会了如何使用使用docker容器运行Python程序,同时也对之前的实验的相关操作进行了复习,对docker的知识掌握得更好了!

  • 相关阅读:
    【LeetCode】Validate Binary Search Tree
    【LeetCode】Search in Rotated Sorted Array II(转)
    【LeetCode】Search in Rotated Sorted Array
    【LeetCode】Set Matrix Zeroes
    【LeetCode】Sqrt(x) (转载)
    【LeetCode】Integer to Roman
    贪心算法
    【LeetCode】Best Time to Buy and Sell Stock III
    【LeetCode】Best Time to Buy and Sell Stock II
    CentOS 6 上安装 pip、setuptools
  • 原文地址:https://www.cnblogs.com/ss333/p/12924584.html
Copyright © 2011-2022 走看看