This commit is contained in:
parent
8a86893048
commit
fa04803943
3 changed files with 59 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -160,3 +160,4 @@ cython_debug/
|
||||||
#.idea/
|
#.idea/
|
||||||
tmp/
|
tmp/
|
||||||
.env
|
.env
|
||||||
|
html/
|
||||||
|
|
14
README.md
14
README.md
|
@ -4,7 +4,19 @@
|
||||||
|
|
||||||
# mcaptcha_api: Python library to interact with mCaptcha server
|
# mcaptcha_api: Python library to interact with mCaptcha server
|
||||||
|
|
||||||
### Example
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install mcaptcha_api
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
| Parameter | Info |
|
||||||
|
| -------------- | --------------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| `instance_url` | the URL of the mCaptcha instance you are using |
|
||||||
|
| `sitekey` | The captcha identifier; can be obtained from dashboard or from widget URL (`http://hostname/widget?sitekey=<sitekey>` |
|
||||||
|
| `secret` | Account secret; can be obtained from mCaptcha dashboard account settings page |
|
||||||
|
|
||||||
```python
|
```python
|
||||||
mcaptcha = MCaptcha(instance_url=INSTANCE_URL, sitekey=SITEKEY, secret=SECRET)
|
mcaptcha = MCaptcha(instance_url=INSTANCE_URL, sitekey=SITEKEY, secret=SECRET)
|
||||||
|
|
|
@ -8,7 +8,41 @@ from requests import Session
|
||||||
|
|
||||||
|
|
||||||
class MCaptcha:
|
class MCaptcha:
|
||||||
|
"""mCaptcha API client class
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Attributes
|
||||||
|
----------
|
||||||
|
instance_url: str
|
||||||
|
the URL of the mCaptcha instance you are using
|
||||||
|
sitekey: str
|
||||||
|
The captcha identifier; can be obtained from dashboard
|
||||||
|
or from widget URL `http://hostname/widget?sitekey=<sitekey>`
|
||||||
|
secret: str
|
||||||
|
Account secret; can be obtained from mCaptcha dashboard
|
||||||
|
account settings page
|
||||||
|
|
||||||
|
Methods
|
||||||
|
-------
|
||||||
|
verify(token)
|
||||||
|
Verify mCaptcha authorization token presented by the visitor
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, instance_url: str, sitekey: str, secret: str):
|
def __init__(self, instance_url: str, sitekey: str, secret: str):
|
||||||
|
"""
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
instance_url: str
|
||||||
|
the URL of the mCaptcha instance you are using
|
||||||
|
sitekey: str
|
||||||
|
The captcha identifier; can be obtained from dashboard
|
||||||
|
or from widget URL `http://hostname/widget?sitekey=<sitekey>`
|
||||||
|
secret: str
|
||||||
|
Account secret; can be obtained from mCaptcha dashboard
|
||||||
|
account settings page
|
||||||
|
"""
|
||||||
|
|
||||||
self.client = Session()
|
self.client = Session()
|
||||||
self.instance_url = self.__clean_url(instance_url)
|
self.instance_url = self.__clean_url(instance_url)
|
||||||
self.sitekey = sitekey
|
self.sitekey = sitekey
|
||||||
|
@ -24,6 +58,17 @@ class MCaptcha:
|
||||||
return urlunparse((parsed.scheme, parsed.netloc, path, "", "", ""))
|
return urlunparse((parsed.scheme, parsed.netloc, path, "", "", ""))
|
||||||
|
|
||||||
def verify(self, token: str) -> bool:
|
def verify(self, token: str) -> bool:
|
||||||
|
"""Verify mCaptcha authorization token presented by the visitor
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
token: authorization token presented by the visitor
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
bool
|
||||||
|
indicating the validity of of the presented authorization presented
|
||||||
|
"""
|
||||||
payload = {"token": token, "key": self.sitekey, "secret": self.secret}
|
payload = {"token": token, "key": self.sitekey, "secret": self.secret}
|
||||||
resp = self.client.post(self.verify_url, json=payload)
|
resp = self.client.post(self.verify_url, json=payload)
|
||||||
assert resp.status_code == 200
|
assert resp.status_code == 200
|
||||||
|
|
Loading…
Add table
Reference in a new issue