client.py 858 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python
  2. import sys
  3. from os.path import abspath, dirname
  4. basepath = abspath(dirname(abspath(__file__)) + '/..')
  5. sys.path.insert(0, basepath)
  6. from websocket import websocket
  7. from connection import Connection
  8. from message import TextMessage
  9. from errors import SocketClosed
  10. ADDR = ('localhost', 8000)
  11. class EchoClient(Connection):
  12. def onopen(self):
  13. print 'Connection established, sending "foo"'
  14. self.send(TextMessage('foo'))
  15. def onmessage(self, msg):
  16. print 'Received', msg
  17. raise SocketClosed(None, 'response received')
  18. def onerror(self, e):
  19. print 'Error:', e
  20. def onclose(self, code, reason):
  21. print 'Connection closed'
  22. if __name__ == '__main__':
  23. print 'Connecting to ws://%s:%d' % ADDR
  24. sock = websocket()
  25. sock.connect(ADDR)
  26. EchoClient(sock).receive_forever()