Package: verify-python
VerifyAn elegant verification code generation framework.ContentsNo.1 BackgroundNo.2 InstallationNo.3 UsageNo.4 ExampleNo.5 Expand&OverwriteNo.6 Configuration tableNo.7 LicenseBackgroundCaptchas are common in web development, but common captchas are easy to identify and bypass. The project provides a simple and flexible captcha generator that is efficient, secure, and easy to extend.InstallationWay 1：pip install verify-pythonWay 2：pipinstallgit+git://github.com/blackmonkey121/verify.gitorpipinstallgit+https//github.com/blackmonkey121/verify.gitWay 3：Download👉Click meDownloadverify-pythonpythonsetup.pyinstal# 在 verify-python 根目录下执行UsageVerifyGiffromverifyimportVerifyGifgif=VerifyGif()# Get the instance of VerifyGif.veri=gif('A48K')# You can specify it yourself or generate it randomly.veri.save_file()# Save the verify code to `.Verify/verifyxxx.gif`veri.get_binary()# Get the verify code format binary.VerifyPngfromverifyimportVerifyPngpng_instance=VerifyPng()veri=instance('J7Wd')veri.get_binary()SafeCreate encrypted all request parameters.fromverifyimportSafes=Safe()ret=s.coding(string='Mst4')# ret : `IntcInN0clwiOiBcIkRcXHUwMG...XCJSU0FcIiwgXCJ2dFwiOiBcImdpZlwifSI=`ret=s.parse(ret)# ret : `{'str': 'Mst4', 'mtd': 'RSA', 'vt': 'gif'}`Configway1Use config file. This is recommended if there are many configuration informationfromverifyimportConfig# You can tell verify configuration information in the form of a file.config=Config('my_conifg')/config=Config('my_conifg.py')# Extend name can be accept.way2Inherit theConfigclass, and then rewrite the attributes. This makes your code look clear, and you can add some methods. But it's a bit complicated.fromverifyimportConfig,VerifyGifclassMyConfig(Config):FRAME_NUMBER:int=24VERIFY_SIZE:tuple=(40,40)LINES_NUMBER:int=4veri=VerifyGif(config=MyConfig)# You must pass it as a parameter to VerifyGif/VerifyPng.way3Add attributes directly toconfig.  This is recommended if there is little configuration information.fromverifyimportconfigconfig.FRAME_NUMBER:int=24config.VERIFY_SIZE:tuple=(42,42)config.LINES_NUMBER:int=5CacheThis is a thread-safe cache. The package itself depends on it, which is convenient if you happen to need it in your project.  For example, save the generated verification code.fromverifyimportCachecache=Cache(contain=1024,expiration=60*60)# contain: Maximum capacity.  expiration: timeout.cache.set('key','value')# set value.cache.get('key')# get value.cache.clear()# clear cache.tips:You have to use your own instantiated object.Examplebusiness serverCreate the<img src="https://path/verify/verify_url_para">fromverifyimportSafes=Safe()verify_url_para=s.coding('S7rE')url=url+verify_url_para# verify_url_para: IntcInN0clwsjMG...XCJSU0FcIiXCJ2dFwiOiBcImdpZlwifSI=rander(<imgsrc="https://path/verify/verify_url_para">)render(<imgsrc="https://xxx/verify/IntcInN0clwsjMG...XCJSU0FcIiXCJ2dFwiOiBcImdpZlwifSI="alt="verify">)verify serverGet the verify_url_para, parse it. Create the verification code binary format.request_str="IntcInN0clwsjMG...XCJSU0FcIiXCJ2dFwiOiBcImdpZlwifSI="fromverifyimportSafe,VerifyGif,VerifyPngs=Safe()request_data=s.parse(request_str)# request_data: {'str': 'S7rE', 'mtd': 'RSA', 'vt': 'gif'}verify_dict={'gif':VerifyGif,'png':VerifyPng}defget_verify_binary(request_data):verify_type=request_data.get('vt',None)string=request_data.get('str',None)verify_cls=verify_dict.get(verify_type,None)ifverify_clsisnotNone:instance=verify_cls()verify=instance(string=string)returnverify.get_binary()else:raiseException('Request parameter error.')verify_binary=get_verify_binary(request_data)returnverify_binary# Verification code in binary format.More examplesExpand & OverwriteProvides support for two types of verification codes, namelyGifVerifyandPngVerify,When instantiating them, you can specifyBuilder、config、Filter、Storage、Style.BuilderCreate_charcreate char image.Create_backgroundcreate background layer.back_fix_charMix character pictures into the background.Filterchar_filterWill be called aftercreate_char.back_filterwill be called aftercreate_background.frame_filerwill be called afterGifVerify/PngVerify.get_frameStoragesave_fileSave theGifVerify/PngVerify objectto the file system.get_binaryReturns the binary data of thisGifVerify/PngVerify object.Styleget_linesLocation informations of interference line.get_positionsChareset position informations of in the background.get_anglesChareset angle informations of in the background.frame_styleAll style information of each layer.You can inherit them and rewrite them to meet your needs. For more detailed introduction, please see thedocument.StoragefromverifyimportVerifyPng,PngStorageclassMyStorage(PngStorage):defshow_img(self):fromPILimportImageimg=Image.open(self.instance)# Read image from self.instanceimg.show()# show the image.veri=VerifyPng(storage=MyStorage)# Instantiate VerifyPngpng=veri('HQ3r')# Return `MyStorage` instance.png.show_img()# Call `show_img` methods.FilterfromverifyimportVerifyPng,PngFilterclassMyFilter(PngFilter):defchar_filter(self,verify:object,char:'Image.Image',*args,**kwargs)->'Image.Image':...print('Overwrite char_filter. ')...returnsuper().char_filter(verify=verify,char=char,*args,**kwargs)veri=VerifyPng(filter=MyFilter)png=veri('HQ3r')BuilderfromverifyimportVerifyPng,PngFrameBuilderclassMyBuilder(PngFrameBuilder):defcreate_background(self,back_filter,*args,**kwargs)->'Image.Image':...print('Overwrite create_background. ')...returnsuper().create_background(back_filter=back_filter,*args,**kwargs)veri=VerifyPng(builder=MyBuilder)png=veri('HQ3r')StylefromverifyimportVerifyPng,PngStyleclassMyStyle(PngStyle):defget_angles(self,*args,**kwargs)->'Iterable':...print('Overwrite get_angles. ')...returnsuper(MyStyle,self).get_angles()veri=VerifyPng(style=MyStyle)png=veri('HQ3r')ConfigConfig will provide a hook that will call all methods ending in_clean. This can dynamically adjust the parameters, especially when the values of other parameters are not clear. You can also filter and add specific parameters.fromverifyimportVerifyPng,ConfigclassMyConfig(Config):VERIFY_SIZE=(200,54)# Write custom configuration informationVERIFY_CODE_NUMBER=6# it will be used first.VERIFY_CODE_SIZE=(50,50)DEFORM_OFFSET=6DEFORM_NUMBER=2# Methods ending in `_clean` will be called by default.defdeform_clean(self):self.DEFORM_NUMBER=1ifself.DEFORM_NUMBER<1elseself.DEFORM_NUMBERveri=VerifyPng(storage=MyStorage,config=MyConfig)png=veri()png.show()You can pass in multiple custom classes at the same time, as long as they follow the corresponding interface....veri=VerifyPng(storage=MyStorage,config=MyConfig,filter=MyFilter,builder=MyBuilder,style=MyStyle)png=veri('H7rJ')png.show()...Follow the interface without inheriting the default class. All interfaces are aggregated inverify.abc.py.Interface list:AbstractVerify,AbstractFilter,AbstractStyle,AbstractStorage,AbstractFrameBuilderfromverify.abcimportAbstractFilterclassMyFilter(AbstractFilter):defback_filter(self,verify:object,back:'Image.Image',*args,**kwargs)->'Image.Image':returnbackdefframe_filter(self,verify:object,*args,**kwargs)->'Image.Image':returnverify.framedefchar_filter(self,verify:object,char:'Image.Image',*args,**kwargs)->'Image.Image':returncharveri=VerifyPng(filter=MyFilter,storage=MyStorage)png=veri('Ag3r')png.show()Configuration tableConfigurationDefaultMeaningVERIFY_CODE_SETNUMBERS + CHARS_BIG + CHARS_LOW <  list >Random character setCHAR_FONTImageFont.truetype('Arial.ttf', 40)Font and sizeVERIFY_CODE_NUMBER4Number of characters on each layerVERIFY_CODE_SIZE(40, 40)  < pixel >Character sizeBACK_COLOR(255, 255, 255, 255)  < pixel >Background colorCHAR_COLOR(0, 0, 0, 255)  < pixel >Character colorNULL_COLOR(0, 0, 0, 0)  < pixel >Null colorVERIFY_SIZE(180, 60)  < pixel >CAPTCHA sizeBACK_NOISE_NUMBER200Number of background noiseBACK_NOISE_TYPE2  < pixel >Size of background noiseLINES_NUMBER4Number of interference linesCHAR_CUT_NUMBER8Number of character fragmentsCHAR_CUT_PRESENT0.2Size of incomplete areaCIRCLE_NUMBER6Number of interference circleFRAME_NUMBER30Frame number of GIF VerifyTRACK_INTERVAL10 < pixel >Character rotation radiusANGLE_INTERVAL60 < ±60 >Rotation range of charactersRSA_FOLDERRSA_KEYRSA key save directoryRSA_KEY_DIRverify.RSA_KEY< builder-in path >RSA key save pathSAFE_ENGINE'RSA'Default encryption engineSECRET_KEY'a-=3bb51t_x#........s4_=w^40xj#7g'Secret key for fast encryption engineSTORAGE_DIR'Verify'Save location of CAPTCHADEFORM_NUMBER2Number of character twistsDEFORM_OFFSET6 < pixel >The degree of character distortionLicenseGPLv 3.0