2

i am having an error with the example code provided and i have not found anything on google, here is the traceback

ERROR:websockets.server:Error in connection handler
Traceback (most recent call last):
  File "C:\Users\felix\AppData\Local\Programs\Python\Python36\lib\site-packages\websockets\server.py", line 84, in handler
    yield from self.ws_handler(self, path)
  File "C:\Users\felix\Desktop\letistry\server.py", line 45, in counter
    async for message in websocket:
TypeError: 'async for' requires an object with __aiter__ method, got WebSocketServerProtocol

all i have done is copy and paste code from https://websockets.readthedocs.io/en/stable/intro.html

im running the Synchronization example (code follows) on python 3.6 on windows 10.

#!/usr/bin/env python

# WS server example that synchronizes state across clients

import asyncio
import json
import logging
import websockets

logging.basicConfig()

STATE = {'value': 0}

USERS = set()

def state_event():
    return json.dumps({'type': 'state', **STATE})

def users_event():
    return json.dumps({'type': 'users', 'count': len(USERS)})

async def notify_state():
    if USERS:       # asyncio.wait doesn't accept an empty list
        message = state_event()
        await asyncio.wait([user.send(message) for user in USERS])

async def notify_users():
    if USERS:       # asyncio.wait doesn't accept an empty list
        message = users_event()
        await asyncio.wait([user.send(message) for user in USERS])

async def register(websocket):
    USERS.add(websocket)
    await notify_users()

async def unregister(websocket):
    USERS.remove(websocket)
    await notify_users()

async def counter(websocket, path):
    # register(websocket) sends user_event() to websocket
    await register(websocket)
    try:
        await websocket.send(state_event())
        async for message in websocket:
            data = json.loads(message)
            if data['action'] == 'minus':
                STATE['value'] -= 1
                await notify_state()
            elif data['action'] == 'plus':
                STATE['value'] += 1
                await notify_state()
            else:
                logging.error(
                    "unsupported event: {}", data)
    finally:
        await unregister(websocket)

asyncio.get_event_loop().run_until_complete(
    websockets.serve(counter, 'localhost', 6789))
asyncio.get_event_loop().run_forever()
6
  • 1
    There are so many examples on the page, I am afraid no one will give suggestions if you cannot clean up and paste them to your question... Commented Oct 9, 2018 at 1:50
  • what needs cleaning up? should i post the code from the example here? thanks for your comment Commented Oct 9, 2018 at 1:53
  • 1
    yeah, this will make your question more likely to be answered, people will move to other posts if they need to find the code from other place with a lots of code. You need to give a minimal workable code which can reproduce your issues. Commented Oct 9, 2018 at 1:56
  • have install other websocket lib? Commented Oct 9, 2018 at 2:33
  • im not sure.. is this likely to make a difference? might they interfere? Commented Oct 9, 2018 at 2:43

1 Answer 1

1

This appears to be a bug in a dependency library, telethon, according to this thread:

https://github.com/expectocode/telegram-export/issues/33

So this may occur if you're using outdated packages that come with your distribution, for example. Using virtualenv and making sure I was using the most recent packages via pip inside the virtual environment solved the problem for me.

Specifically, the version installed with package python3-websockets on Ubuntu 18.04 is currently 3.4-1. Using pip in a virtual environment, the version is 8.0.3.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.