Создание одноразовых URL на Python
Мне нужно создавать одноразовый URL который будет неактивен после перехода на него. Нет возможности использовать бд. Возможно кто-нибудь подскажет источник или скорректирует по библиотекам которые можно использовать?
Ответы (1 шт):
Автор решения: eri
→ Ссылка
Код
from aiohttp import web
import uuid
routes = web.RouteTableDef()
@routes.get('/')
def genlink(request):
orig = request.query.get('orig')
uid = uuid.uuid4()
url = f"/{uid.hex}"
request.app.cache[uid]=orig
return web.Response(text=url)
@routes.get('/{uid}')
def golink(request):
uid = uuid.UUID(request.match_info.get('uid'))
orig = request.app.cache.pop(uid, None)
if orig:
raise web.HTTPFound(location=orig)
else:
raise web.HTTPNotFound()
app = web.Application()
app.cache = dict()
app.add_routes(routes)
web.run_app(app)
Формируем ссылку
$ curl -vv "http://127.0.0.1:8080/?orig=http%3A%2F%2Fipinfo.io/ip"
* Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /?orig=http%3A%2F%2Fipinfo.io/ip HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.82.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=utf-8
< Content-Length: 33
< Date: Thu, 07 Apr 2022 09:40:33 GMT
< Server: Python/3.10 aiohttp/3.8.1
<
* Connection #0 to host 127.0.0.1 left intact
/b8b95ba24f764cf7873217cfa9e01cd7
И переход
$ curl -L -vv http://127.0.0.1:8080/b8b95ba24f764cf7873217cfa9e01cd7
* Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /b8b95ba24f764cf7873217cfa9e01cd7 HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.82.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 Found
< Content-Type: text/plain; charset=utf-8
< Location: http://ipinfo.io/ip
< Content-Length: 10
< Date: Thu, 07 Apr 2022 09:41:29 GMT
< Server: Python/3.10 aiohttp/3.8.1
<
* Ignoring the response-body
* Connection #0 to host 127.0.0.1 left intact
* Issue another request to this URL: 'http://ipinfo.io/ip'
* Trying 34.117.59.81:80...
* Connected to ipinfo.io (34.117.59.81) port 80 (#1)
> GET /ip HTTP/1.1
> Host: ipinfo.io
> User-Agent: curl/7.82.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< access-control-allow-origin: *
< content-type: text/html; charset=utf-8
< content-length: 13
< date: Thu, 07 Apr 2022 09:41:29 GMT
< x-envoy-upstream-service-time: 1
< Via: 1.1 google
<
* Connection #1 to host ipinfo.io left intact
9x.29.xxx.10x
А вот просрочка при повторе
$ curl -L -vv http://127.0.0.1:8080/b8b95ba24f764cf7873217cfa9e01cd7
* Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /b8b95ba24f764cf7873217cfa9e01cd7 HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.82.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< Content-Length: 14
< Date: Thu, 07 Apr 2022 09:41:56 GMT
< Server: Python/3.10 aiohttp/3.8.1
<
* Connection #0 to host 127.0.0.1 left intact
404: Not Found