Python generates Verification Code instances and python Verification Code instances
This example shows how to generate a verification code using Python, which is of great practical value. Share it with you for your reference. The specific implementation method is as follows:
The front-end Page code is as follows:
<Div> <! -- Any time random number (timestamp) to prevent the verification code from being updated due to the page cache --> <a href = "javascript: refreshCode (); "rel =" external nofollow "style =" color: blue; "> cannot see another image </a> </div> <script> function refreshCode () {$ ('authcode _ img '). src = "/registration/makeimage/" + Math. random () ;}</script>
The background program is as follows:
Import StringIOimport Image, ImageDraw, ImageFont, and random # the corresponding module must be installed from xxx. settings import authcode_font # Make sure that the modified font contains def make_image (request): mp = hashlib. md5 () mp. update (str (datetime. datetime. now () + str (random. random () mp_src = mp. hexdigest () rand_str = mp_src [0: 6] font = ImageFont. truetype (authcode_font, 25) width = 75 height = 30 im = Image. new ('rgb ', (width, height),' # % s' % mp_src [-7:-1]) draw = ImageDraw. draw (im) draw. line (random. randint (0, width), random. randint (0, height), random. randint (0, width), random. randint (0, height) draw. line (random. randint (0, width), random. randint (0, height), random. randint (0, width), random. randint (0, height) draw. line (random. randint (0, width), random. randint (0, height), random. randint (0, width), random. randint (0, height) draw. line (random. randint (0, width), random. randint (0, height), random. randint (0, width), random. randint (0, height) draw. line (random. randint (0, width), random. randint (0, height), random. randint (0, width), random. randint (0, height) draw. text (5, 2), rand_str, font = font) del draw buffer = StringIO. stringIO () im. save (buffer, 'jpeg ') httpResponse = HttpResponse (content = buffer. getvalue (), mimetype = "image/jpeg") request. session ['auth _ Code'] = rand_str return httpResponse
The program effect is as follows:
How can I handle the verification code for python Renren's Automatic Logon? Otherwise, automatic logon cannot be implemented.
What should I do if I encounter a verification code? There are two solutions:
-
1. google's verification code, cool
-
2. simple verification code: the number of characters is limited. It only uses simple translation or rotation to add noise without being distorted. This kind of verification code can still be processed. The general idea is to rotate it back, noise Removal,
Divide a single character, divide it, and then use the feature extraction method (such as PCA) to reduce the dimension and generate a feature library, and then compare the verification code with the feature library. This is complicated. I can't finish a blog post.
.
-
3. In fact, some verification codes are still very weak. I won't name them here. Anyway, I have extracted very accurate verification codes through the 2 method, SO 2 is actually feasible.
How can I obtain the verification code image through python?
Because the verification code image is not cached, it cannot be obtained using the cache function. There are many solutions. You can use the same session to request an image again. For more information about where such images are stored, see