ckbunker
ckbunker copied to clipboard
make the catcha appear in python 3.11
On Python 3.11 the captcha doesn't appear anymore:
As this error happens:
500 Internal Server Error
Traceback:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
resp = await request_handler(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/web_app.py", line 504, in _handle
resp = await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp/web_middlewares.py", line 117, in impl
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/aiohttp_session/__init__.py", line 199, in factory
response = await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/me/Projects/ckbunker/webapp.py", line 799, in auth_everything
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/me/Projects/ckbunker/webapp.py", line 273, in captcha_image
itype, data = MegaGifCaptcha(seed=code).draw(code, foreground='#444')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/me/Projects/ckbunker/make_captcha.py", line 93, in draw
ch = ''.join(sample(charset, 1)).upper()
^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/random.py", line 436, in sample
raise TypeError("Population must be a sequence. "
TypeError: Population must be a sequence. For dicts or sets, use sorted(d).
Then, I found in the changelog:
The population parameter of [random.sample()](https://docs.python.org/3/library/random.html#random.sample)
must be a sequence, and automatic conversion of [set](https://docs.python.org/3/library/stdtypes.html#set)s
to [list] (https://docs.python.org/3/library/stdtypes.html#list)s is no longer supported.
So I changed ch = ''.join(sample(charset, 1)).upper()
to ch = ''.join(sample(list(charset), 1)).upper()
and now the captcha shows again: