Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Websockets Module has no attribute

I have this python script that sends a WebSocket message to my python server. But it keeps throwing errors. I have seen these errors before, but I can't seem to fix it. I use the 2.7.x version.

#!/usr/bin/python
import websocket
import sys
val = sys.argv[1]
ws = websocket.create_connection("ws://ipaddress:9001")
ws.send(val)
ws.close()

The Error

Traceback (most recent call last):
  File "./test.py", line 5, in <module>
    ws = websocket.create_connection("ws://ipaddress:9001")
AttributeError: 'module' object has no attribute 'create_connection'
like image 320
user7063014 Avatar asked Oct 24 '16 06:10

user7063014


2 Answers

you've installed the wrong library (websocket) try installing websocket-client

$ pip install websocket-client

and then your code must work just fine

like image 93
mehdy Avatar answered Sep 18 '22 05:09

mehdy


See the answer by falsetru here: AttributeError: 'module' object has no attribute 'WebSocketApp'

" Make sure that you didn't name your file as websocket.py; Otherwise, it will prevent import of the desired third-party module websocket; because your module is searched first according to sys.path module search path.

Rename your module to other name, and make sure to clean websocket.pyc if it was generated."

like image 29
skeller88 Avatar answered Sep 21 '22 05:09

skeller88