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
    Results are run on Digital Ocean.
    3 replies
    I see now 🤦‍♂️ my bad
    Marwan Rabbâa
    @waghanza
    :-)
    Ahmed
    @ahmedhr

    debug sanic with pycharm when debug=False
    , if we want to keep debug=True we also have to explicitly mention auto_reload=False

    Microsoft/vscode-python#2316

    Is this a bug?

    Adam Hopkins
    @ahopkins
    nope
    that is intentional
    debug mode automatically enables auto reload on purpose
    if you only want the debug logging, and not reload, then you need to be explicit.
    Ahmed
    @ahmedhr
    Alright, makes sense. Thanks!
    Andrew Bastien
    @volundmush
    Sanic peoples! I am curious
    So I am looking at the 'Custom Protocols' thing and am wondering about just how far those can go. For instance, what about protocols that don't necessarily generate a response - such as Telnet? I am currently working on something MUD-related and want to use modern Python to give it web features so am looking at various ASGI technologies built on Asyncio for this purpose....
    it seems like every single ASGI-related tool and library assumes HTTP+WebSockets. which, I mean, that's a fairly sensible assumption to make I suppose, but...
    I'm imagining a server that opens up several ports, each serving different protocols - one is telnet, another is ssh, then a third is HTTP+WS...
    Adam Hopkins
    @ahopkins
    First, Sanic is not an ASGI server. You would need to use uvicorn, hypercorn, or daphne if you wanted ASGI. With that said, I am not familiar enough with the internals of any of those tools of they can be configured to handle any service other than http and websockets.
    Sanic is an ASGI framework. But, as such, it would exist only in the http and websockets world.
    Second, Sanic does ship with its own web server
    This is not based upon the ASGI event system.
    It is based directly upon asyncio.Protocol
    While I have never done either of those, if definitely should be possible
    Andrew Bastien
    @volundmush
    yeah I am starting to understand more about how this works after reading a bit more. ASGI was confusing me a bit but I see the difference between server and app now
    Adam Hopkins
    @ahopkins
    :+1: I am really interested to hear how this comes out. If you do come up with something, would you mind sharing in the community forums?
    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.