[Python] 파이썬 CAPTCHA 만들기
개발을 하다 보면 Captcha를 적용해야 하는 경우가 있다.
물론 Google reCaptcha 혹은 Naver Captcha와 같이 이미 제공을 하는 곳이 많이 있다.
하지만 제공하는 것을 모든 환경에서 사용을 할 수 없기 때문에 간단히 파이썬을 이용하여 Captcha를 만들어 보고자 한다.
1. captcha 라이브러리 설치
우선 사용을 위해서는 captcha 라이브러리를 설치해야 한다.
설치 방법은 pip 명령어를 통해서 아래와 같이 쉽게 설치가 가능하다.
- $ pip install captcha
# pip를 이용한 captcha 라이브러리 설치
$ pip install captcha
Collecting captcha
Using cached captcha-0.5.0-py3-none-any.whl.metadata (2.1 kB)
Requirement already satisfied: Pillow in /Users/happylie/.pyenv/versions/3.11.0/envs/venv.python_ex/lib/python3.11/site-packages (from captcha) (9.3.0)
Using cached captcha-0.5.0-py3-none-any.whl (102 kB)
Installing collected packages: captcha
Successfully installed captcha-0.5.0
$ pip freeze | grep captcha
captcha==0.5.0
2. captcha 라이브러리 이용하기
이제 captcha 라이브러리를 설치했으면 파이썬 코드를 이용하여 음성(Audio) captcha와 이미지(Image) captcha를 만들어 보도록 하자.
2.1 음성(Audio) captcha 만들기
이제 첫 번째로 간단하게 음성(Audio) captcha를 만들어 보도록 하자.
음성의 경우는 숫자만 가능하니 참고하도록 하자.
# Python Example Code
# Audio captcha
# -*- coding: utf-8 -*-
from captcha.audio import AudioCaptcha
audio = AudioCaptcha()
captcha_text = '1234'
audio.write(captcha_text, 'sample/1234.wav')
2.2 이미지(Image) captcha 만들기
그럼 두 번째로 간단하게 이미지(Image) captcha를 만들어 보도록 하자.
이미지 captcha의 경우 두 가지 방식으로 사용이 가능하다.
- 이미지 파일로 바로 저장하는 방법
- Base64를 통해서 이미지를 data로 만들어 String을 통해서 사용하는 방법
# Python Example Code
# Image captcha
# -*- coding: utf-8 -*-
import io
import base64
from captcha.image import ImageCaptcha
image = ImageCaptcha()
captcha_text = '1234'
image.write(captcha_text, 'sample/1234.png')
data = image.generate(captcha_text)
image_data_b64 = "data:image/png;base64," + base64.b64encode(io.BytesIO(data.read()).getvalue()).decode(
"utf-8")
print(image_data_b64)
### Result ###
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAA8CAIAAABuCSZCAAAg3ElEQVR4nM19yZNk53Ff5re89+rV2tXr9CwABsuAAkmI4CKKWihNSKYth4KOcAQPtv4BHR3hgy8+6eCLwjf/BzoxfLAWm5IjCJEyKUrCgCBBEBxAA8zea3V1Vb1Xb/mWTB9eVU1PT+/dYzq7DxUdU9+WX+aXyy9zsOQELo4UwEd39fi/laPHtywqBGKGSNS++Kc3wkuagS9wrudNCGjH7of/4X0/8iABgbVXu3r88n9a+Pwbn3XgftkLPBGpCxzLjX3ad+r9fmI2vAyQPAAKEOELKnmc6fkmBniB0z0nYs82cWXPbv1jv//ToQRBkgAYGI3wcS2ivwp3wt3GC7HU8pe92OPpIhjMYMfWJj7fLDd+sDv6NPWZJfICBDAwMiMzMAM8V/ayB/IA1Z0667bYsx244Z300V9v5VulSx1ZBgRghGr9Y97Z7UX3oX7pGmr8/18nnY/BBCZ1buTH6/nmP2yNHzk78jY1QAyIzADIgADP+RRMbmzizNCZvhRKtq4qPSeFPvV1cmNvB3b3w2Ttu9t5r3QZIQAAAjMCAjAjMJL2KjQhA138Tp4mdmxH1qbO5TQVDtR1qZtKN9UJxeVcDPYOxuvmwXf77n5aDguTOuZqWsSKuwAwueTPhcdkyCYu3cru/9MjuifIiAgj++tzi19owIIQp3kRyHLZt4/+emv488QkzueEgMiTn2pPwCyEqOm4udCUVjExiOexrQn5sU9+kaz/3Y4pCRCQERDjldrlf7Gg6wpO9j6cncEI4HO/88Akn4zsRiFgooOr45ieCgA8LwGmksod2/vx7tqPembX2rEDK8qgDL8TKFTzvx5DcIrR2PLux/nw43G5bYkJABFm/H1qA0TAdwCWGZoHXyCbTOwv3TyX/Nix23pvMPh0bMYeEZERAdGjHTnmk753Z7+BLqeiZ4qPBn5kmYkBgBHhKbY+V9VM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ACRhQChQAsQgCxZBDoUAWKAQghCT+gxFBABRYQRqKasr0ZhNySGaZ0LExADVdh3CRIAQYCK5cKvdl7595fn/6CtLgmoX/g+JlTJw73/vl7uuMd/0zMjt/a3FyMVB/PJZS7v5b0Pd7xxUxww6IZWbaFO3FflAjonn4AYQKHCkD2QFDIINCpQoaytxq50LiHnjS99WAvia3HRtzqC9uuNvG9MYlEwECCIKgWLCMweGWVNhk2tm1F8KVr5+nzjSu1Usf7T0l7fZvhRWp3VRUnFQefOYMdu6x92ig1Hk5aYXHWuu/Qbi0HngD7Jz9J56gRPQgwsQ1Hrxo2XStvzQmO0GpiBC7vBwq+2y55hwe1XG95Q8iiTGtRI1z5TA8Vm16iO5AE5Y4lYIExSdQwylropdUPJSC281W5ci4OO0k11/iLgY2mm7V745srgdnqBUoHP/td2COice++//Hz0gZFCARIBMHG0qL/6Xz93MY1zLpQY4L3/ePv1P36h/nJ8Es9Ngfrg9ocf/0mvTREgE8waifNX/vSN+mrtl+L+vf2tWze//aULH/b/Aue8Z7RE7yFwAAAAAElFTkSuQmCC
3. 기본 코드를 이용한 간단한 라이브러리 제작
아래 코드는 기본 코드를 이용해서 좀 더 쉽게 사용하기 위해서 자체적으로 작성한 코드이다.
해당 코드에 대해서 간단하게 설명을 하자면, 우선 음성(Audio) / 이미지(Image) 모두 불러서 사용할 수 있도록 되어있다.
여기서 음성(Audio)의 경우 숫자로만 이뤄져야 음성(Audio) 파일을 만들 수 있기 때문에 random code 부분에 captcha_type을 추가해 놓았다.
그리고 이미지(Image) captcha의 경우는 이미지 크기, 이미지에 들어가 글자의 크기를 정할 수 있도록 설정해 놓았다.
# -*- coding: utf-8 -*-
import random
import io
import base64
import string
from captcha.image import ImageCaptcha
from captcha.audio import AudioCaptcha
def _random_code(length=8, captcha_type=None):
"""
Random Code Generate
:param length: Default Length 8
:captcha_type: None, audio
:return:
"""
custom_punctuation = '#%&?$'
custom_lowercase = 'abdefghjqrt'
custom_uppercase = 'ABDEFGHJLQRT'
custom_digits = '2345678'
if captcha_type == 'audio':
return "".join(random.choice(string.digits) for _ in range(length))
allowed_code = custom_digits + custom_uppercase + custom_lowercase + custom_punctuation
return "".join(random.choice(allowed_code) for _ in range(length))
class MakeCaptchaAudio:
def __init__(self):
self.audio = AudioCaptcha()
self.captcha_text = _random_code(captcha_type="audio")
self.ret = ""
def set_save_audio_file(self) -> bool:
"""
Set Captcha Audio File Save(File Type : WAV)
:return:
"""
try:
self.audio.write(self.captcha_text, f'sample/output.wav')
return True
except Exception as err:
return False
class MakeCaptchaImg:
def __init__(self):
self.width: int = 250
self.height: int = 100
self.font_sizes: tuple = (50, 58, 64)
self.image = ImageCaptcha(self.width, self.height, font_sizes=self.font_sizes)
self.audio = AudioCaptcha()
self.captcha_text = _random_code()
self.ret = ""
def captcha_valid(self, text) -> bool:
"""
Captcha Valid
:param text:
:return:
"""
if self.captcha_text != text:
return False
return True
def get_base64_image_data(self):
"""
Get Captcha Base64 Image Data(File Type : PNG)
:return:
"""
image_data = self.image.generate(self.captcha_text)
image_data_b64 = "data:image/png;base64," + base64.b64encode(io.BytesIO(image_data.read()).getvalue()).decode(
"utf-8")
return image_data_b64
def set_save_image_file(self) -> bool:
"""
Set Captcha Image File Save(File Type : PNG)
:return:
"""
try:
self.image.write(self.captcha_text, f'sample/output.png')
return True
except Exception as err:
return False
if __name__ == "__main__":
# CAPTCHA Audio
captcha_audio = MakeCaptchaAudio()
print(f'CAPTCHA Text: {captcha_audio.captcha_text}')
print(f'Save Audio: {captcha_audio.set_save_audio_file()}')
# CAPTCHA Image
captcha_img = MakeCaptchaImg()
print(f'CAPTCHA Text: {captcha_img.captcha_text}')
print(f'Base64 Image: {captcha_img.get_base64_image_data()}')
print(f'Save Image: {captcha_img.set_save_image_file()}')
print(f'CAPTCHA Valid: {captcha_img.captcha_valid(captcha_img.captcha_text)}')
print(f'CAPTCHA Valid: {captcha_img.captcha_valid("1234")}')
### Result ###
CAPTCHA Text: 92992991
Save Audio: True
CAPTCHA Text: %dDg62dh
Base64 Image : data:image/png;base64,iVBOR...QmCC
Save Image: True
CAPTCHA Valid: True
CAPTCHA Valid: False
4. 예제 코드
captcha 라이브러리를 이용하여 음성(Audio) / 이미지(Image) captcha를 만들어 보는 방법을 알아보았다.
위에 나온 예제 파일은 Github에 올려놓았다.
해당 내용에 대해서 문의가 있거나 궁금한 내용이 있다면 Github Issus 혹은 댓글로 남겨주면 최대한 빠르게 답변을 할 수 있도록 하겠다.
🌵댓글