by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Adam Hopkins
    @ahopkins
    I guess the main distinction worth pointing out is that Sanic is first and foremost a server and web framework.
    It also just so happens to allow for the "framework" to also be hooked into an ASGI server if you need
    rlundy82
    @rlundy82
    I'm new to async development. I'm coming from a Flask(ish) background, so I'm loving the way Sanic looks to me. I've got a question regarding Sanic-WTF.
    I've not been able to get it to work, I keep getting errors while following the examples. My question is if there's any updated docs or alternatives or... what's the best practices when dealing with form submission?
    rlundy82
    @rlundy82
    I just spotted in the forums that it's a dead package. =/
    Here is a list of some Sanic packages.
    Obviously not everything is there.
    There's also a discussion where I think that package did come up. https://community.sanicframework.org/t/move-essential-sanic-libraries-under-sanic-organisation/567
    rlundy82
    @rlundy82

    I'm having an issue with request.ctx and middleware.

    @app.middleware('request')
    async def _prepare(request):
        request.ctx.test = '1234'
    
    @app.middleware('response')
    async def _cleanup(request, response):
        test = request.ctx.test
        #test = getattr(request.ctx, 'test')

    I'm getting this error
    AttributeError: 'types.SimpleNamespace' object has no attribute 'test'

    I guess my question is if this is the same ctx as in _prepare, or if I have to do anything extra to get _cleanup the same info

    rlundy82
    @rlundy82
    I'm getting request.ctx.test just fine in the handler
    rlundy82
    @rlundy82

    I've made this small example to help recreate the issue.

    from sanic import Sanic
    from sanic.response import HTTPResponse
    
    app = Sanic("middleware_test")
    
    @app.middleware("request")
    async def _prepare(request):
        request.ctx.test = 'ASDF'
    
    @app.middleware("response")
    async def _cleanup(request, response):
        test = request.ctx.test
    
    @app.route("/")
    async def test(request):
        test = request.ctx.test
        return HTTPResponse("Got Test: %s" % test, content_type='text/html')
    
    if __name__ == "__main__":
        app.run(host="0.0.0.0", port=8888)

    If you run the code, you'll notice the handler renders the response just fine, but if you look at the console output you'll notice the traceback:

    [2020-07-04 21:45:19 +0000] [21208] [ERROR] Exception occurred in one of response middleware handlers
    Traceback (most recent call last):
      File "<path>/env/lib/python3.6/site-packages/sanic/app.py", line 977, in handle_request
        request, response, request_name=name
      File "<path>/env/lib/python3.6/site-packages/sanic/app.py", line 1281, in _run_response_middleware
        _response = await _response
      File "mwtest.py", line 12, in _cleanup
        test = request.ctx.test
    AttributeError: 'types.SimpleNamespace' object has no attribute 'test'
    Justin
    @Paladiamors

    Got a quick question about csrf tokens for sanic. With the 20.3 release, looks like sanic-wtf no longer works and there is a pull request that hasn't been merged. pyx/sanic-wtf#17

    The developer doesn't seem to be active anymore, are there any alternatives to this library?

    Adam Hopkins
    @ahopkins
    Tomorrow I am giving a talk at EuroPython and I will touch this a little.
    But, afterwards I will post some examples that will include A way to do this
    Justin
    @Paladiamors
    @ahopkins Thanks for posting that information. The Fernet method of creating the token + encrypted version of the the token looks very neat.
    Adam Hopkins
    @ahopkins
    😎
    Adam Hopkins
    @ahopkins
    I am working on a solution based on this as a pip installable.
    Justin
    @Paladiamors

    @ahopkins Awesome I hope your talk went well, if there is a video of it online, I'd be happy to give it a watch. I ended up throwing the functions into a class and it worked great.

    One minor thing I noticed:

    https://github.com/ahopkins/europython2020-overcoming-access-control/blob/master/examples/server-with-sanic-jwt.py#L109

    decoded = cipher.decrypt(token) -> decoded = cipher.decrypt(token, ttl)

    Will help make the ttl active :)

    Adam Hopkins
    @ahopkins
    Thanks. Yes I will share the link when it is ready. Definitely agree with the TTL for production 🤙
    rlundy82
    @rlundy82
    Sanic website down?
    Adam Hopkins
    @ahopkins
    I'll check
    thanks for the heads up
    Adam Hopkins
    @ahopkins
    back online
    Rado Kozmer
    @rkozmer
    Hi! Does anybody know how to properly access database (RocksDB) when having multiple Sanic workers. Db writes are obviously cached per process and it leads to unpredictable results when reading from the db. With 4 Sanic workers I get randomly one of the last 4 writen versions of data. Thank you for any hint.
    Adam Hopkins
    @ahopkins
    not familiar with rocksdb
    but from a brief look at their website, it looks like it is a local dB
    and not a service
    you would need to create some sort of a wrapper to be able to expose this in a distributed system
    otherwise each worker has its own dB instance
    Rado Kozmer
    @rkozmer
    Thank you @ahopkins Yes. I'm not familiar with Python multiprocessing, but is there a way to spawn a dedicated process for db and somehow talk to it from all Sanic worker processes (a kind of inter-process communication)? Otherwise I have to create a socket and some comm. protocol.
    Adam Hopkins
    @ahopkins
    You could create a simple tcp server. Python generally makes it easy. A quick Google search came up with this https://gist.github.com/Integralist/3f004c3594bbf8431c15ed6db15809ae
    I would, however highly recommend Redis instead. It is meant for this sort of scalable key/value store
    Rado Kozmer
    @rkozmer
    @ahopkins Thank you!
    Rado Kozmer
    @rkozmer
    Hi! One question regarding sanic-jwt. What is the proper way to get user's identity in the protected route? Thanks.
    Rado Kozmer
    @rkozmer
    Ok, found it.