zoukankan      html  css  js  c++  java
  • python安装及写一个简单的验证码组件(配合node)

    1.安装Python

    到官网下载响应系统的版本(这里以windows为例):https://www.python.org/downloads/windows/

    然后就是不断地“下一步”

    2.运行

    要想node那样可以在命令行运行,要首先配置好系统的全局环境变量:

    win10为例:“此电脑”--->“属性”--->“高级系统设置”--->“高级”下的“环境变量”--->找到“path”--->添加两个变量(python的安装目录和这个目录下的Scripts)

    打开cmd,打python就可以运行了,同时python包管理器pip也可以运行了


    3.开发验证码组件

    先下载pillow库:

    pip install pillow

    photo.py:

    # 引入pillow库
    from PIL import Image,ImageDraw,ImageFont
    # 引入randint用于生成随机int
    from random import randint
    # 引入sys
    import sys
    
    w = int(sys.argv[1])
    h = int(sys.argv[2])
    fs = int(sys.argv[3])
    s = sys.argv[4]
    
    # 用PIL的Image创建画布(颜色模式,(宽,高),(颜色对应最大取值))
    img = Image.new('RGB',(w,h),(255,255,255))
    
    # 用PIL的ImageDraw创建画笔gd
    gd = ImageDraw.Draw(img)
    
    # 循环把文字输出
    for i in range(len(s)):
        # for下面记得缩进,不然报错
        # 画文字
        gd.text(
            # 起始坐标点
            (i*w/len(s)+randint(8,15),(h-fs)/2+randint(-5,5)),
            # 要画的文字
            s[i],
            # 颜色
            (randint(0,255),randint(0,255),randint(0,255)),
            # 字体
            ImageFont.truetype("C:\Windows\Fonts\BuxtonSketch.ttf",fs)
        )
    
    for i in range(20):
        # for下面记得缩进,不然报错    
        # 画线
        gd.line(
            # 起点和终点 坐标点
            (randint(0,w),randint(0,h),randint(0,w),randint(0,h)),
            # 颜色
            (randint(0,255),randint(0,255),randint(0,255)),
            # 线宽度
            1
        )
    
    for i in range(int(w*h/200)):
        # for下面记得缩进,不然报错    
        # 画点
        gd.point(
            # 坐标
            (randint(0,w),randint(0,h)),
            # 颜色
            (randint(0,255),randint(0,255),randint(0,255))
        )
    
    # 保存图片
    img.save('1.png','PNG')

    运行命令如:

    python photo.py 200 100 50 sdfs

    就会生成  宽高200,100  字体50  内容为“sdfs”的验证码

    4.配合node使用

    const koa = require('koa')
    const koaRouter = require('koa-router')
    const cp = require('child_process')
    const fs = require('fs')
    const static = require('koa-static')
    
    const server = new koa()
    server.listen(8088)
    
    const r = new koaRouter()
    server.use(r.routes())
    
    r.get('/verify_code', async ctx => {
        let {w,h,fs} = ctx.query
        w = w ? w : 200
        h = h ? h : 100
        fs = fs ? fs : 50
        let seeds = 'abcdefghijklimnopqrstuvwxyzABCDEFGHIJKLMNOPQRST2345678'
        let arr = []
        for(let i=0;i<5;i++) {
            arr.push(seeds[parseInt(seeds.length*Math.random())])
        }
        let data = await newVerify(w,h,fs,arr.join('')).catch(e=>{})
        ctx.body = data
    })
    
    function newVerify(w,h,fontSize,str) {
        return new Promise(async (resolve,reject)=> {
            cp.exec(`python photo.py ${w} ${h} ${fontSize} ${str}`, err => {
                if(err) {
                    reject()
                }
                fs.readFile('./1.png', (err, data) => {
                    if(err) {
                        reject()
                    } else {
                        resolve(data)
                    }
                })
            })
        })
    }
    
    server.use(static('./www'))
  • 相关阅读:
    Spark-sql windows 下 执行错误.
    notepad ++ 注册表
    log4j 配置文件 示例
    linux 查看 进程 内存占用
    spring boot 常见错误解决
    python 轻量 web 框架 Bottle 使用
    Spring cloud eureka 添加 spring-security
    vue can‘ not resolver sass-loader 的 解决办法。
    外国人眼中的珍珠奶茶是啥?
    75.2亿美元:诺基亚、微软终于在一起
  • 原文地址:https://www.cnblogs.com/amiezhang/p/8643087.html
Copyright © 2011-2022 走看看