Python module for easy integration with 2Captcha API service to solve various types of captchas including reCAPTCHA, FunCaptcha, GeeTest, and many others
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
The core solver API provides the foundation for all captcha solving operations through the TwoCaptcha class. It handles initialization, generic solving methods, account management, and error handling.
Main solver class that provides both high-level captcha-specific methods and low-level generic solving capabilities.
class TwoCaptcha:
def __init__(self, apiKey, softId=4580, callback=None,
defaultTimeout=120, recaptchaTimeout=600,
pollingInterval=10, server='2captcha.com',
extendedResponse=None):
"""
Initialize the 2captcha solver.
Parameters:
- apiKey (str): Your 2captcha API key (required)
- softId (int): Software developer ID (default: 4580)
- callback (str): URL for pingback when captcha is solved
- defaultTimeout (int): Default timeout in seconds for most captcha types (default: 120)
- recaptchaTimeout (int): Timeout in seconds for reCAPTCHA (default: 600)
- pollingInterval (int): Interval in seconds between result polling (default: 10)
- server (str): 2captcha server domain (default: '2captcha.com')
- extendedResponse (bool): Return extended response format
"""Core methods for submitting captchas and retrieving results, providing manual control over the solving process.
def solve(self, timeout=0, polling_interval=0, **kwargs):
"""
Generic solve method that submits captcha and waits for result.
Parameters:
- timeout (int): Override default timeout (0 = use default)
- polling_interval (int): Override default polling interval (0 = use default)
- **kwargs: Captcha-specific parameters
Returns:
dict: {'captchaId': str, 'code': str} or extended format if enabled
"""
def send(self, **kwargs):
"""
Manually submit captcha without waiting for result.
Parameters:
- method (str): Captcha type method
- **kwargs: Captcha-specific parameters
Returns:
str: Captcha ID for later result retrieval
"""
def get_result(self, id_):
"""
Manually poll for captcha result.
Parameters:
- id_ (str): Captcha ID returned from send() method
Returns:
str: Solution code or dict if extendedResponse enabled
"""Methods for managing your 2captcha account including balance checking and solution quality reporting.
def balance(self):
"""
Get current account balance.
Returns:
float: Account balance in USD
"""
def report(self, id_, correct):
"""
Report captcha solution quality for refund eligibility.
Parameters:
- id_ (str): Captcha ID of solved captcha
- correct (bool): True if solution was correct, False if incorrect
"""Internal utility methods used by the solver but not intended for direct use by end users. These methods are accessible but primarily used internally by the TwoCaptcha class.
def wait_result(self, id_, timeout, polling_interval):
"""
Internal method to wait for captcha result with custom timeout and polling.
Parameters:
- id_ (str): Captcha ID
- timeout (int): Maximum wait time in seconds
- polling_interval (int): Seconds between polls
Returns:
str: Solution code or dict if extendedResponse enabled
Note: This is an internal method. Use solve() for automatic solving.
"""
def get_method(self, file):
"""
Internal method to determine API submission method based on file input.
Parameters:
- file (str): File path or base64 string
Returns:
dict: Method parameters for API submission
Note: This is an internal method used by captcha solving methods.
"""from twocaptcha import TwoCaptcha
# Initialize solver
solver = TwoCaptcha('your_api_key')
# Automatic solving (submit and wait)
try:
result = solver.solve(method='post', file='captcha.jpg')
print(f"Solved: {result['code']}")
except Exception as e:
print(f"Error: {e}")from twocaptcha import TwoCaptcha, TimeoutException
solver = TwoCaptcha('your_api_key')
try:
# Submit captcha
captcha_id = solver.send(method='post', file='captcha.jpg')
print(f"Submitted: {captcha_id}")
# Wait for result with custom timeout
result = solver.wait_result(captcha_id, timeout=60, polling_interval=5)
print(f"Solved: {result}")
except TimeoutException:
print("Captcha solving timed out")
except Exception as e:
print(f"Error: {e}")from twocaptcha import TwoCaptcha
solver = TwoCaptcha('your_api_key')
# Check balance
balance = solver.balance()
print(f"Balance: ${balance}")
# Report incorrect solution for refund
solver.report('captcha_id_here', correct=False)from twocaptcha import TwoCaptcha
# Custom timeouts and server
solver = TwoCaptcha(
apiKey='your_api_key',
defaultTimeout=180, # 3 minutes for normal captchas
recaptchaTimeout=900, # 15 minutes for reCAPTCHA
pollingInterval=5, # Check every 5 seconds
server='rucaptcha.com', # Alternative server
extendedResponse=True # Get detailed response format
)Install with Tessl CLI
npx tessl i tessl/pypi-2captcha-python