Python implementation simple image Verification code login

Source: Internet
Author: User

Friends said that the company to test the environment to do interface testing, login need to pass the correct image of the verification Code, the principle of lazy and easy, recommend him to test the environment of the picture verification code to write dead , our company is doing so ^_^. Persuade no fruit/(ㄒoㄒ)/~~, only through the OCR technology to identify the picture verification code, looked at their verification code, long so, fortunately very easy to identify (background color is transparent, there is a pit to deal with).

Python implementation of the image verification Code login demo, the use of the third-party module has requests, PIL, Pytesseract.

1 #Coding:utf-82 ImportRequests3  fromPILImportImage4  fromPytesseractImportimage_to_string, Pytesseract5 6Pytesseract.tesseract_cmd ='d:\\env\\tesseract-ocr\\tesseract'7 #URL8Base_url ='https://hostxxx'9Code_url = Base_url +'/common-platform/code'TenDo_login_url = Base_url +'/common-platform/dologin' One  A  - #Blank background color pytesseract not recognized, changed the background color - defChange_background (IMG_FP): the     Try: -IMG =Image.open (IMG_FP) -X, y =img.size -New_img = Image.new ('RGBA', Img.size, (255, 255, 255)) + New_img.paste (IMG, (0, 0, x, y), IMG) -         returnnew_img +     except: A         PrintU'failed to change picture background' at  -  - #Identify the image verification code - defocr2str (IMG): -     returnstr (image_to_string (IMG)) -  in  - #Create session toSession =requests. Session () + #request Picture Verification Code Interface -Code_resp = Session.request (method='GET', Url=code_url, verify=False) the #Save picture Verification Code *With open ('Code.png','WB') as F: $ f.write (code_resp.content)Panax Notoginseng #Verification Code -Code = OCR2STR (Change_background ('Code.png')) the #Login Data +Do_login_data = { A     'UserName':'User', the     'Password':'pwd', +     'Verificationcode': Code - } $ #Login Interface $Do_login_resp = Session.request (method='POST', Url=do_login_url, Data=do_login_data, verify=False) - PrintDo_login_resp.text#Verify that the login is successful

Python implementation simple image Verification code login

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.