README.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. =================
  2. websocket-client
  3. =================
  4. websocket-client module is WebSocket client for python. This provide the low level APIs for WebSocket. All APIs are the synchronous functions.
  5. websocket-client supports only hybi-13.
  6. License
  7. ============
  8. - LGPL
  9. Installation
  10. =============
  11. This module is tested on only Python 2.7.
  12. Type "python setup.py install" or "pip install websocket-client" to install.
  13. This module does not depend on any other module.
  14. How about Python 3
  15. ===========================
  16. py3( https://github.com/liris/websocket-client/tree/py3 ) branch is for python 3.3. Every test case is passed.
  17. If you are using python3, please check it.
  18. Example
  19. ============
  20. Low Level API example::
  21. from websocket import create_connection
  22. ws = create_connection("ws://echo.websocket.org/")
  23. print "Sending 'Hello, World'..."
  24. ws.send("Hello, World")
  25. print "Sent"
  26. print "Reeiving..."
  27. result = ws.recv()
  28. print "Received '%s'" % result
  29. ws.close()
  30. If you want to customize socket options, set sockopt.
  31. sockopt example:
  32. from websocket import create_connection
  33. ws = create_connection("ws://echo.websocket.org/".
  34. sockopt=((socket.IPPROTO_TCP, socket.TCP_NODELAY),) )
  35. JavaScript websocket-like API example::
  36. import websocket
  37. import thread
  38. import time
  39. def on_message(ws, message):
  40. print message
  41. def on_error(ws, error):
  42. print error
  43. def on_close(ws):
  44. print "### closed ###"
  45. def on_open(ws):
  46. def run(*args):
  47. for i in range(3):
  48. time.sleep(1)
  49. ws.send("Hello %d" % i)
  50. time.sleep(1)
  51. ws.close()
  52. print "thread terminating..."
  53. thread.start_new_thread(run, ())
  54. if __name__ == "__main__":
  55. websocket.enableTrace(True)
  56. ws = websocket.WebSocketApp("ws://echo.websocket.org/",
  57. on_message = on_message,
  58. on_error = on_error,
  59. on_close = on_close)
  60. ws.on_open = on_open
  61. ws.run_forever()
  62. wsdump.py
  63. ============
  64. wsdump.py is simple WebSocket test(debug) tool.
  65. sample for echo.websocket.org::
  66. $ wsdump.py ws://echo.websocket.org/
  67. Press Ctrl+C to quit
  68. > Hello, WebSocket
  69. < Hello, WebSocket
  70. > How are you?
  71. < How are you?
  72. Usage
  73. ---------
  74. usage::
  75. wsdump.py [-h] [-v [VERBOSE]] ws_url
  76. WebSocket Simple Dump Tool
  77. positional arguments:
  78. ws_url websocket url. ex. ws://echo.websocket.org/
  79. optional arguments:
  80. -h, --help show this help message and exit
  81. -v VERBOSE, --verbose VERBOSE set verbose mode. If set to 1, show opcode. If set to 2, enable to trace websocket module
  82. example::
  83. $ wsdump.py ws://echo.websocket.org/
  84. $ wsdump.py ws://echo.websocket.org/ -v
  85. $ wsdump.py ws://echo.websocket.org/ -vv
  86. ChangeLog
  87. ============
  88. - v0.12.0
  89. - support keep alive for WebSocketApp(ISSUE#34)
  90. - fix some SSL bugs(ISSUE#35, #36)
  91. - fix "Timing out leaves websocket library in bad state"(ISSUE#37)
  92. - fix "WebSocketApp.run_with_no_err() silently eats all exceptions"(ISSUE#38)
  93. - WebSocketTimeoutException will be raised for ws/wss timeout(ISSUE#40)
  94. - improve wsdump message(ISSUE#42)
  95. - support fragmentation message(ISSUE#43)
  96. - fix some bugs
  97. - v0.11.0
  98. - Only log non-normal close status(ISSUE#31)
  99. - Fix default Origin isn't URI(ISSUE#32)
  100. - fileno support(ISSUE#33)
  101. - v0.10.0
  102. - allow to set HTTP Header to WebSocketApp(ISSUE#27)
  103. - fix typo in pydoc(ISSUE#28)
  104. - Passing a socketopt flag to the websocket constructor(ISSUE#29)
  105. - websocket.send fails with long data(ISSUE#30)
  106. - v0.9.0
  107. - allow to set opcode in WebSocketApp.send(ISSUE#25)
  108. - allow to modify Origin(ISSUE#26)
  109. - v0.8.0
  110. - many bug fix
  111. - some performance improvement
  112. - v0.7.0
  113. - fixed problem to read long data.(ISSUE#12)
  114. - fix buffer size boundary violation
  115. - v0.6.0
  116. - Patches: UUID4, self.keep_running, mask_key (ISSUE#11)
  117. - add wsdump.py tool
  118. - v0.5.2
  119. - fix Echo App Demo Throw Error: 'NoneType' object has no attribute 'opcode (ISSUE#10)
  120. - v0.5.1
  121. - delete invalid print statement.
  122. - v0.5.0
  123. - support hybi-13 protocol.
  124. - v0.4.1
  125. - fix incorrect custom header order(ISSUE#1)