Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError during WebSockets example in Web Server quickstart docs #3234

Closed
kaganjd opened this issue Sep 2, 2018 · 3 comments
Closed

NameError during WebSockets example in Web Server quickstart docs #3234

kaganjd opened this issue Sep 2, 2018 · 3 comments
Labels

Comments

@kaganjd
Copy link

kaganjd commented Sep 2, 2018

Long story short

Thanks for the very awesome lib! I just wrestled with the WebSockets quickstart docs for a long time and wanted to report a solution that worked for me in case others run into this:

async def websocket_handler(request):

    ws = web.WebSocketResponse()
    await ws.prepare(request)

    async for msg in ws:
        if msg[0].value == 1:  # TEXT according to here: https://github.com/aio-libs/aiohttp/blob/3a65252aa4c822056b408caa883fbdbf78311fa6/aiohttp/http_websocket.py#L41
            if msg[1] == 'close':
                await ws.close()
            else:
                await ws.send_str(msg[1] + '/answer')
        elif msg[0].value == 102: # ERROR according to here: https://github.com/aio-libs/aiohttp/blob/3a65252aa4c822056b408caa883fbdbf78311fa6/aiohttp/http_websocket.py#L41
            print('ws connection closed with exception %s' %
                  ws.exception())

    print('websocket connection closed')

    return ws

The way the docs are currently written led me to this error, I think because the msg attributes are accessed differently than described:

    if msg.type == aiohttp.WSMsgType.TEXT:
NameError: name 'aiohttp' is not defined

Expected behaviour

Actual behaviour

Steps to reproduce

Your environment

aiohttp 3.4.2- server on localhost
python 3.6

@kxepal
Copy link
Member

kxepal commented Sep 2, 2018

You should make import aiohttp to resolve this issue. Eventually, you did from aiohttp import web, but this bit is missing.

@kaganjd
Copy link
Author

kaganjd commented Sep 2, 2018

Ah, you're totally right! Thanks, closing.

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants