Python installs and writes a simple verification code component (with node)

Source: Internet
Author: User

1. Install Python

Download the response System version to the official website (for example, Windows here): https://www.python.org/downloads/windows/

And then there's the "Next Step",

2. Running

To be able to run at the command line like node, first configure the system's Global environment variables:

Win10 For example: "This Computer"---> Properties---> Advanced system settings---> "Environment variables" under "Advanced"---> Find "path"---> Add two variables (Python's installation directory and scripts in this directory)

Open cmd, Python can run, and Python Package Manager Pip can also run the

3. Developing the CAPTCHA component

Download the Pillow Library First:

Pip Install Pillow

photo.py:

#Introducing the Pillow library fromPILImportImage,imagedraw,imagefont#introduction of Randint for generating random int fromRandomImportRandint#Introducing SYSImportSYSW= Int (sys.argv[1]) H= Int (sys.argv[2]) FS= Int (sys.argv[3]) s= Sys.argv[4]#Create canvas with PIL image (color mode, (width, height), (color corresponds to maximum value))img = image.new ('RGB', (W,h), (255,255,255))#Create a brush with PiL Imagedraw GDGD =Imagedraw.draw (IMG)#loop to output text forIinchRange (len (s)):#for the following remember indent, or error    #Drawing TextGd.text (#Starting coordinate point(I*w/len (s) +randint (8,15), (H-FS)/2+randint ( -5,5)),        #the text to be drawnS[i],#Color(Randint (0,255), Randint (0,255), Randint (0,255)),        #FontImagefont.truetype ("C:\\windows\\fonts\\buxtonsketch.ttf", FS)) forIinchRange (20):    #for the following remember indent, or error    #Draw LineGd.line (#start and end coordinate points(Randint (0,w), Randint (0,h), Randint (0,w), Randint (0,h)),#Color(Randint (0,255), Randint (0,255), Randint (0,255)),        #line width1    ) forIinchRange (int (w*h/200)):    #for the following remember indent, or error    #Draw pointsGd.point (#coordinates(Randint (0,w), Randint (0,h)),#Color(Randint (0,255), Randint (0,255), Randint (0,255))    )#Save PictureImg.save ('1.png','PNG')

Run commands such as:

 $  -  SDFs

Generates a 200,100 font 50 code with the content "SDFs"

4. Use with 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=NewKOA () Server.listen (8088) Const R=NewKoarouter () Server.use (R.routes ()) R.get ('/verify_code ', async CTX ={let {w,h,fs}=ctx.query W= W? w:200h= h? h:100FS= FS? Fs:50Let 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})functionnewverify (w,h,fontsize,str) {return NewPromise (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 '))

Python installs and writes a simple verification code component (with node)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.