ImportError: No module named twisted.websocket

#1

Hi,

I just started trying to get the hang of websockets, and I’m getting the following error for one of the examples of a client using autobahn and twisted:

Traceback (most recent call last):

File “./autobahn.py”, line 3, in

from autobahn.twisted.websocket import WebSocketClientProtocol,WebSocketClientFactory

File “/home/user/websockets/autobahn.py”, line 3, in

from autobahn.twisted.websocket import WebSocketClientProtocol,WebSocketClientFactory

ImportError: No module named twisted.websocket

I googled around and found there were changes to the way you would import from previous versions, but this seems to be the correct way. Any idea of what I may be doing wrong?

This is the example I’m trying:

#!/usr/bin/env python2.7

from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketClientFactory

class MyClientProtocol(WebSocketClientProtocol):

def onConnect(self, response):

  print("Server connected: {0}".format(response.peer))

def onOpen(self):

  print("WebSocket connection open.")

  def hello():

     self.sendMessage(u"Hello, world!".encode('utf8'))

     self.sendMessage(b"\x00\x01\x03\x04", isBinary = True)

     self.factory.reactor.callLater(1, hello)

  ## start sending messages every second ..

  hello()

def onMessage(self, payload, isBinary):

  if isBinary:

     print("Binary message received: {0} bytes".format(len(payload)))

  else:

     print("Text message received: {0}".format(payload.decode('utf8')))

def onClose(self, wasClean, code, reason):

  print("WebSocket connection closed: {0}".format(reason))

if name == ‘main’:

import sys

from twisted.python import log

from twisted.internet import reactor

log.startLogging(sys.stdout)

factory = WebSocketClientFactory(“ws://localhost:9000”, debug = False)

factory.protocol = MyClientProtocol

reactor.connectTCP(“127.0.0.1”, 9000, factory)

reactor.run()

I followed the instructions for installing autobahn via pip, these are the versions I have installed:

GnuPGInterface==0.3.2

Twisted==13.2.0

argparse==1.2.1

asyncio==0.4.1

autobahn==0.8.8

backports.ssl-match-hostname==3.4.0.2

command-not-found==0.2.44

distribute==0.7.3

futures==2.1.6

gevent==1.0.1

greenlet==0.4.2

language-selector==0.1

pycares==0.6.1

pycurl==7.19.0

python-apt==0.8.3ubuntu7.2

six==1.6.1

tornado==3.2

ufw==0.31.1-1

unattended-upgrades==0.1

websocket==0.2.1

websocket-client==0.13.0

ws4py==0.3.4

wsgiref==0.1.2

zope.interface==4.1.1

0 Likes

#2

To make sure my environment wasn’t busted, I also created a virtualenv and installed autobahn[twisted,accelerate] from scratch:

Twisted==13.2.0

argparse==1.2.1

autobahn==0.8.8

distribute==0.7.3

six==1.6.1

ujson==1.33

wsaccel==0.6.2

wsgiref==0.1.2

zope.interface==4.1.1

But the result was the same…

···

On Tuesday, May 6, 2014 3:04:43 PM UTC-4, Pedro Abreu wrote:

Hi,

I just started trying to get the hang of websockets, and I’m getting the following error for one of the examples of a client using autobahn and twisted:

Traceback (most recent call last):

File “./autobahn.py”, line 3, in

from autobahn.twisted.websocket import WebSocketClientProtocol,WebSocketClientFactory

File “/home/user/websockets/autobahn.py”, line 3, in

from autobahn.twisted.websocket import WebSocketClientProtocol,WebSocketClientFactory

ImportError: No module named twisted.websocket

I googled around and found there were changes to the way you would import from previous versions, but this seems to be the correct way. Any idea of what I may be doing wrong?

This is the example I’m trying:

#!/usr/bin/env python2.7

from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketClientFactory

class MyClientProtocol(WebSocketClientProtocol):

def onConnect(self, response):

  print("Server connected: {0}".format(response.peer))

def onOpen(self):

  print("WebSocket connection open.")
  def hello():
     self.sendMessage(u"Hello, world!".encode('utf8'))
     self.sendMessage(b"\x00\x01\x03\x04", isBinary = True)
     self.factory.reactor.callLater(1, hello)
  ## start sending messages every second ..
  hello()

def onMessage(self, payload, isBinary):

  if isBinary:
     print("Binary message received: {0} bytes".format(len(payload)))
  else:
     print("Text message received: {0}".format(payload.decode('utf8')))

def onClose(self, wasClean, code, reason):

  print("WebSocket connection closed: {0}".format(reason))

if name == ‘main’:

import sys

from twisted.python import log

from twisted.internet import reactor

log.startLogging(sys.stdout)

factory = WebSocketClientFactory(“ws://localhost:9000”, debug = False)

factory.protocol = MyClientProtocol

reactor.connectTCP(“127.0.0.1”, 9000, factory)

reactor.run()

I followed the instructions for installing autobahn via pip, these are the versions I have installed:

GnuPGInterface==0.3.2

Twisted==13.2.0

argparse==1.2.1

asyncio==0.4.1

autobahn==0.8.8

backports.ssl-match-hostname==3.4.0.2

command-not-found==0.2.44

distribute==0.7.3

futures==2.1.6

gevent==1.0.1

greenlet==0.4.2

language-selector==0.1

pycares==0.6.1

pycurl==7.19.0

python-apt==0.8.3ubuntu7.2

six==1.6.1

tornado==3.2

ufw==0.31.1-1

unattended-upgrades==0.1

websocket==0.2.1

websocket-client==0.13.0

ws4py==0.3.4

wsgiref==0.1.2

zope.interface==4.1.1

0 Likes

#3

Hi,

    WebSocketClientProtocol,WebSocketClientFactory
       File "/home/user/websockets/autobahn.py", line 3, in <module>

Mmh. Perhaps name your test file differently (not "autobahn.py") .. probably Python is confused trying to lookup Autobahn things within your script/module ..

Tobias

0 Likes

#4

Wow, that was exactly it… Highly noobish on my part.

Thanks Tobias!

···

On Wednesday, May 7, 2014 12:07:48 PM UTC-4, Tobias Oberstein wrote:

Hi,

WebSocketClientProtocol,WebSocketClientFactory
   File "/home/user/websockets/autobahn.py", line 3, in <module>

Mmh. Perhaps name your test file differently (not “autobahn.py”) …
probably Python is confused trying to lookup Autobahn things within your
script/module …

Tobias

0 Likes

#5

    Hi,

     > WebSocketClientProtocol,WebSocketClientFactory
     > File "/home/user/websockets/autobahn.py", line 3, in <module>

    Mmh. Perhaps name your test file differently (not "autobahn.py") ..
    probably Python is confused trying to lookup Autobahn things within
    your
    script/module ..

    Tobias

Wow, that was exactly it... Highly noobish on my part.

Thanks Tobias!

Pedro,

Great it works for you now! No problem - I think I have fallen into that nasty trap myself before;)

Tobias

···

Am 07.05.2014 18:12, schrieb Pedro Abreu:

On Wednesday, May 7, 2014 12:07:48 PM UTC-4, Tobias Oberstein wrote:

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com
<mailto:autobahnws+...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

0 Likes