websocket.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. """
  2. websocket - WebSocket client library for Python
  3. Copyright (C) 2010 Hiroki Ohtani(liris)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. """
  16. import socket
  17. try:
  18. import ssl
  19. from ssl import SSLError
  20. HAVE_SSL = True
  21. except ImportError:
  22. # dummy class of SSLError for ssl none-support environment.
  23. class SSLError(Exception):
  24. pass
  25. HAVE_SSL = False
  26. from urlparse import urlparse
  27. import os
  28. import array
  29. import struct
  30. import uuid
  31. import hashlib
  32. import base64
  33. import threading
  34. import time
  35. import logging
  36. import traceback
  37. import sys
  38. """
  39. websocket python client.
  40. =========================
  41. This version support only hybi-13.
  42. Please see http://tools.ietf.org/html/rfc6455 for protocol.
  43. """
  44. # websocket supported version.
  45. VERSION = 13
  46. # closing frame status codes.
  47. STATUS_NORMAL = 1000
  48. STATUS_GOING_AWAY = 1001
  49. STATUS_PROTOCOL_ERROR = 1002
  50. STATUS_UNSUPPORTED_DATA_TYPE = 1003
  51. STATUS_STATUS_NOT_AVAILABLE = 1005
  52. STATUS_ABNORMAL_CLOSED = 1006
  53. STATUS_INVALID_PAYLOAD = 1007
  54. STATUS_POLICY_VIOLATION = 1008
  55. STATUS_MESSAGE_TOO_BIG = 1009
  56. STATUS_INVALID_EXTENSION = 1010
  57. STATUS_UNEXPECTED_CONDITION = 1011
  58. STATUS_TLS_HANDSHAKE_ERROR = 1015
  59. logger = logging.getLogger()
  60. class WebSocketException(Exception):
  61. """
  62. websocket exeception class.
  63. """
  64. pass
  65. class WebSocketConnectionClosedException(WebSocketException):
  66. """
  67. If remote host closed the connection or some network error happened,
  68. this exception will be raised.
  69. """
  70. pass
  71. class WebSocketTimeoutException(WebSocketException):
  72. """
  73. WebSocketTimeoutException will be raised at socket timeout during read/write data.
  74. """
  75. pass
  76. default_timeout = None
  77. traceEnabled = False
  78. def enableTrace(tracable):
  79. """
  80. turn on/off the tracability.
  81. tracable: boolean value. if set True, tracability is enabled.
  82. """
  83. global traceEnabled
  84. traceEnabled = tracable
  85. if tracable:
  86. if not logger.handlers:
  87. logger.addHandler(logging.StreamHandler())
  88. logger.setLevel(logging.DEBUG)
  89. def setdefaulttimeout(timeout):
  90. """
  91. Set the global timeout setting to connect.
  92. timeout: default socket timeout time. This value is second.
  93. """
  94. global default_timeout
  95. default_timeout = timeout
  96. def getdefaulttimeout():
  97. """
  98. Return the global timeout setting(second) to connect.
  99. """
  100. return default_timeout
  101. def _parse_url(url):
  102. """
  103. parse url and the result is tuple of
  104. (hostname, port, resource path and the flag of secure mode)
  105. url: url string.
  106. """
  107. if ":" not in url:
  108. raise ValueError("url is invalid")
  109. scheme, url = url.split(":", 1)
  110. parsed = urlparse(url, scheme="http")
  111. if parsed.hostname:
  112. hostname = parsed.hostname
  113. else:
  114. raise ValueError("hostname is invalid")
  115. port = 0
  116. if parsed.port:
  117. port = parsed.port
  118. is_secure = False
  119. if scheme == "ws":
  120. if not port:
  121. port = 80
  122. elif scheme == "wss":
  123. is_secure = True
  124. if not port:
  125. port = 443
  126. else:
  127. raise ValueError("scheme %s is invalid" % scheme)
  128. if parsed.path:
  129. resource = parsed.path
  130. else:
  131. resource = "/"
  132. if parsed.query:
  133. resource += "?" + parsed.query
  134. return (hostname, port, resource, is_secure)
  135. def create_connection(url, timeout=None, **options):
  136. """
  137. connect to url and return websocket object.
  138. Connect to url and return the WebSocket object.
  139. Passing optional timeout parameter will set the timeout on the socket.
  140. If no timeout is supplied, the global default timeout setting returned by getdefauttimeout() is used.
  141. You can customize using 'options'.
  142. If you set "header" list object, you can set your own custom header.
  143. >>> conn = create_connection("ws://echo.websocket.org/",
  144. ... header=["User-Agent: MyProgram",
  145. ... "x-custom: header"])
  146. timeout: socket timeout time. This value is integer.
  147. if you set None for this value, it means "use default_timeout value"
  148. options: current support option is only "header".
  149. if you set header as dict value, the custom HTTP headers are added.
  150. """
  151. sockopt = options.get("sockopt", [])
  152. sslopt = options.get("sslopt", {})
  153. websock = WebSocket(sockopt=sockopt, sslopt=sslopt)
  154. websock.settimeout(timeout if timeout is not None else default_timeout)
  155. websock.connect(url, **options)
  156. return websock
  157. _MAX_INTEGER = (1 << 32) -1
  158. _AVAILABLE_KEY_CHARS = range(0x21, 0x2f + 1) + range(0x3a, 0x7e + 1)
  159. _MAX_CHAR_BYTE = (1<<8) -1
  160. # ref. Websocket gets an update, and it breaks stuff.
  161. # http://axod.blogspot.com/2010/06/websocket-gets-update-and-it-breaks.html
  162. def _create_sec_websocket_key():
  163. uid = uuid.uuid4()
  164. return base64.encodestring(uid.bytes).strip()
  165. _HEADERS_TO_CHECK = {
  166. "upgrade": "websocket",
  167. "connection": "upgrade",
  168. }
  169. class ABNF(object):
  170. """
  171. ABNF frame class.
  172. see http://tools.ietf.org/html/rfc5234
  173. and http://tools.ietf.org/html/rfc6455#section-5.2
  174. """
  175. # operation code values.
  176. OPCODE_CONT = 0x0
  177. OPCODE_TEXT = 0x1
  178. OPCODE_BINARY = 0x2
  179. OPCODE_CLOSE = 0x8
  180. OPCODE_PING = 0x9
  181. OPCODE_PONG = 0xa
  182. # available operation code value tuple
  183. OPCODES = (OPCODE_CONT, OPCODE_TEXT, OPCODE_BINARY, OPCODE_CLOSE,
  184. OPCODE_PING, OPCODE_PONG)
  185. # opcode human readable string
  186. OPCODE_MAP = {
  187. OPCODE_CONT: "cont",
  188. OPCODE_TEXT: "text",
  189. OPCODE_BINARY: "binary",
  190. OPCODE_CLOSE: "close",
  191. OPCODE_PING: "ping",
  192. OPCODE_PONG: "pong"
  193. }
  194. # data length threashold.
  195. LENGTH_7 = 0x7d
  196. LENGTH_16 = 1 << 16
  197. LENGTH_63 = 1 << 63
  198. def __init__(self, fin=0, rsv1=0, rsv2=0, rsv3=0,
  199. opcode=OPCODE_TEXT, mask=1, data=""):
  200. """
  201. Constructor for ABNF.
  202. please check RFC for arguments.
  203. """
  204. self.fin = fin
  205. self.rsv1 = rsv1
  206. self.rsv2 = rsv2
  207. self.rsv3 = rsv3
  208. self.opcode = opcode
  209. self.mask = mask
  210. self.data = data
  211. self.get_mask_key = os.urandom
  212. def __str__(self):
  213. return "fin=" + str(self.fin) \
  214. + " opcode=" + str(self.opcode) \
  215. + " data=" + str(self.data)
  216. @staticmethod
  217. def create_frame(data, opcode):
  218. """
  219. create frame to send text, binary and other data.
  220. data: data to send. This is string value(byte array).
  221. if opcode is OPCODE_TEXT and this value is uniocde,
  222. data value is conveted into unicode string, automatically.
  223. opcode: operation code. please see OPCODE_XXX.
  224. """
  225. if opcode == ABNF.OPCODE_TEXT and isinstance(data, unicode):
  226. data = data.encode("utf-8")
  227. # mask must be set if send data from client
  228. return ABNF(1, 0, 0, 0, opcode, 1, data)
  229. def format(self):
  230. """
  231. format this object to string(byte array) to send data to server.
  232. """
  233. if any(x not in (0, 1) for x in [self.fin, self.rsv1, self.rsv2, self.rsv3]):
  234. raise ValueError("not 0 or 1")
  235. if self.opcode not in ABNF.OPCODES:
  236. raise ValueError("Invalid OPCODE")
  237. length = len(self.data)
  238. if length >= ABNF.LENGTH_63:
  239. raise ValueError("data is too long")
  240. frame_header = chr(self.fin << 7
  241. | self.rsv1 << 6 | self.rsv2 << 5 | self.rsv3 << 4
  242. | self.opcode)
  243. if length < ABNF.LENGTH_7:
  244. frame_header += chr(self.mask << 7 | length)
  245. elif length < ABNF.LENGTH_16:
  246. frame_header += chr(self.mask << 7 | 0x7e)
  247. frame_header += struct.pack("!H", length)
  248. else:
  249. frame_header += chr(self.mask << 7 | 0x7f)
  250. frame_header += struct.pack("!Q", length)
  251. if not self.mask:
  252. return frame_header + self.data
  253. else:
  254. mask_key = self.get_mask_key(4)
  255. return frame_header + self._get_masked(mask_key)
  256. def _get_masked(self, mask_key):
  257. s = ABNF.mask(mask_key, self.data)
  258. return mask_key + "".join(s)
  259. @staticmethod
  260. def mask(mask_key, data):
  261. """
  262. mask or unmask data. Just do xor for each byte
  263. mask_key: 4 byte string(byte).
  264. data: data to mask/unmask.
  265. """
  266. _m = array.array("B", mask_key)
  267. _d = array.array("B", data)
  268. for i in xrange(len(_d)):
  269. _d[i] ^= _m[i % 4]
  270. return _d.tostring()
  271. class WebSocket(object):
  272. """
  273. Low level WebSocket interface.
  274. This class is based on
  275. The WebSocket protocol draft-hixie-thewebsocketprotocol-76
  276. http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
  277. We can connect to the websocket server and send/recieve data.
  278. The following example is a echo client.
  279. >>> import websocket
  280. >>> ws = websocket.WebSocket()
  281. >>> ws.connect("ws://echo.websocket.org")
  282. >>> ws.send("Hello, Server")
  283. >>> ws.recv()
  284. 'Hello, Server'
  285. >>> ws.close()
  286. get_mask_key: a callable to produce new mask keys, see the set_mask_key
  287. function's docstring for more details
  288. sockopt: values for socket.setsockopt.
  289. sockopt must be tuple and each element is argument of sock.setscokopt.
  290. sslopt: dict object for ssl socket option.
  291. """
  292. def __init__(self, get_mask_key=None, sockopt=None, sslopt=None):
  293. """
  294. Initalize WebSocket object.
  295. """
  296. if sockopt is None:
  297. sockopt = []
  298. if sslopt is None:
  299. sslopt = {}
  300. self.connected = False
  301. self.sock = socket.socket()
  302. for opts in sockopt:
  303. self.sock.setsockopt(*opts)
  304. self.sslopt = sslopt
  305. self.get_mask_key = get_mask_key
  306. # Buffers over the packets from the layer beneath until desired amount
  307. # bytes of bytes are received.
  308. self._recv_buffer = []
  309. # These buffer over the build-up of a single frame.
  310. self._frame_header = None
  311. self._frame_length = None
  312. self._frame_mask = None
  313. self._cont_data = None
  314. def fileno(self):
  315. return self.sock.fileno()
  316. def set_mask_key(self, func):
  317. """
  318. set function to create musk key. You can custumize mask key generator.
  319. Mainly, this is for testing purpose.
  320. func: callable object. the fuct must 1 argument as integer.
  321. The argument means length of mask key.
  322. This func must be return string(byte array),
  323. which length is argument specified.
  324. """
  325. self.get_mask_key = func
  326. def gettimeout(self):
  327. """
  328. Get the websocket timeout(second).
  329. """
  330. return self.sock.gettimeout()
  331. def settimeout(self, timeout):
  332. """
  333. Set the timeout to the websocket.
  334. timeout: timeout time(second).
  335. """
  336. self.sock.settimeout(timeout)
  337. timeout = property(gettimeout, settimeout)
  338. def connect(self, url, **options):
  339. """
  340. Connect to url. url is websocket url scheme. ie. ws://host:port/resource
  341. You can customize using 'options'.
  342. If you set "header" dict object, you can set your own custom header.
  343. >>> ws = WebSocket()
  344. >>> ws.connect("ws://echo.websocket.org/",
  345. ... header={"User-Agent: MyProgram",
  346. ... "x-custom: header"})
  347. timeout: socket timeout time. This value is integer.
  348. if you set None for this value,
  349. it means "use default_timeout value"
  350. options: current support option is only "header".
  351. if you set header as dict value,
  352. the custom HTTP headers are added.
  353. """
  354. hostname, port, resource, is_secure = _parse_url(url)
  355. # TODO: we need to support proxy
  356. self.sock.connect((hostname, port))
  357. if is_secure:
  358. if HAVE_SSL:
  359. if self.sslopt is None:
  360. sslopt = {}
  361. else:
  362. sslopt = self.sslopt
  363. self.sock = ssl.wrap_socket(self.sock, **sslopt)
  364. else:
  365. raise WebSocketException("SSL not available.")
  366. self._handshake(hostname, port, resource, **options)
  367. def _handshake(self, host, port, resource, **options):
  368. sock = self.sock
  369. headers = []
  370. headers.append("GET %s HTTP/1.1" % resource)
  371. headers.append("Upgrade: websocket")
  372. headers.append("Connection: Upgrade")
  373. if port == 80:
  374. hostport = host
  375. else:
  376. hostport = "%s:%d" % (host, port)
  377. headers.append("Host: %s" % hostport)
  378. if "origin" in options:
  379. headers.append("Origin: %s" % options["origin"])
  380. else:
  381. headers.append("Origin: http://%s" % hostport)
  382. key = _create_sec_websocket_key()
  383. headers.append("Sec-WebSocket-Key: %s" % key)
  384. headers.append("Sec-WebSocket-Version: %s" % VERSION)
  385. if "header" in options:
  386. headers.extend(options["header"])
  387. headers.append("")
  388. headers.append("")
  389. header_str = "\r\n".join(headers)
  390. self._send(header_str)
  391. if traceEnabled:
  392. logger.debug("--- request header ---")
  393. logger.debug(header_str)
  394. logger.debug("-----------------------")
  395. status, resp_headers = self._read_headers()
  396. if status != 101:
  397. self.close()
  398. raise WebSocketException("Handshake Status %d" % status)
  399. success = self._validate_header(resp_headers, key)
  400. if not success:
  401. self.close()
  402. raise WebSocketException("Invalid WebSocket Header")
  403. self.connected = True
  404. def _validate_header(self, headers, key):
  405. for k, v in _HEADERS_TO_CHECK.iteritems():
  406. r = headers.get(k, None)
  407. if not r:
  408. return False
  409. r = r.lower()
  410. if v != r:
  411. return False
  412. result = headers.get("sec-websocket-accept", None)
  413. if not result:
  414. return False
  415. result = result.lower()
  416. value = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
  417. hashed = base64.encodestring(hashlib.sha1(value).digest()).strip().lower()
  418. return hashed == result
  419. def _read_headers(self):
  420. status = None
  421. headers = {}
  422. if traceEnabled:
  423. logger.debug("--- response header ---")
  424. while True:
  425. line = self._recv_line()
  426. if line == "\r\n":
  427. break
  428. line = line.strip()
  429. if traceEnabled:
  430. logger.debug(line)
  431. if not status:
  432. status_info = line.split(" ", 2)
  433. status = int(status_info[1])
  434. else:
  435. kv = line.split(":", 1)
  436. if len(kv) == 2:
  437. key, value = kv
  438. headers[key.lower()] = value.strip().lower()
  439. else:
  440. raise WebSocketException("Invalid header")
  441. if traceEnabled:
  442. logger.debug("-----------------------")
  443. return status, headers
  444. def send(self, payload, opcode=ABNF.OPCODE_TEXT):
  445. """
  446. Send the data as string.
  447. payload: Payload must be utf-8 string or unicoce,
  448. if the opcode is OPCODE_TEXT.
  449. Otherwise, it must be string(byte array)
  450. opcode: operation code to send. Please see OPCODE_XXX.
  451. """
  452. frame = ABNF.create_frame(payload, opcode)
  453. if self.get_mask_key:
  454. frame.get_mask_key = self.get_mask_key
  455. data = frame.format()
  456. length = len(data)
  457. if traceEnabled:
  458. logger.debug("send: " + repr(data))
  459. while data:
  460. l = self._send(data)
  461. data = data[l:]
  462. return length
  463. def send_binary(self, payload):
  464. return self.send(payload, ABNF.OPCODE_BINARY)
  465. def ping(self, payload=""):
  466. """
  467. send ping data.
  468. payload: data payload to send server.
  469. """
  470. self.send(payload, ABNF.OPCODE_PING)
  471. def pong(self, payload):
  472. """
  473. send pong data.
  474. payload: data payload to send server.
  475. """
  476. self.send(payload, ABNF.OPCODE_PONG)
  477. def recv(self):
  478. """
  479. Receive string data(byte array) from the server.
  480. return value: string(byte array) value.
  481. """
  482. opcode, data = self.recv_data()
  483. return data
  484. def recv_data(self):
  485. """
  486. Recieve data with operation code.
  487. return value: tuple of operation code and string(byte array) value.
  488. """
  489. while True:
  490. frame = self.recv_frame()
  491. if not frame:
  492. # handle error:
  493. # 'NoneType' object has no attribute 'opcode'
  494. raise WebSocketException("Not a valid frame %s" % frame)
  495. elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT):
  496. if frame.opcode == ABNF.OPCODE_CONT and not self._cont_data:
  497. raise WebSocketException("Illegal frame")
  498. if self._cont_data:
  499. self._cont_data[1] += frame.data
  500. else:
  501. self._cont_data = [frame.opcode, frame.data]
  502. if frame.fin:
  503. data = self._cont_data
  504. self._cont_data = None
  505. return data
  506. elif frame.opcode == ABNF.OPCODE_CLOSE:
  507. self.send_close()
  508. return (frame.opcode, None)
  509. elif frame.opcode == ABNF.OPCODE_PING:
  510. self.pong(frame.data)
  511. def recv_frame(self):
  512. """
  513. recieve data as frame from server.
  514. return value: ABNF frame object.
  515. """
  516. # Header
  517. if self._frame_header is None:
  518. self._frame_header = self._recv_strict(2)
  519. b1 = ord(self._frame_header[0])
  520. fin = b1 >> 7 & 1
  521. rsv1 = b1 >> 6 & 1
  522. rsv2 = b1 >> 5 & 1
  523. rsv3 = b1 >> 4 & 1
  524. opcode = b1 & 0xf
  525. b2 = ord(self._frame_header[1])
  526. has_mask = b2 >> 7 & 1
  527. # Frame length
  528. if self._frame_length is None:
  529. length_bits = b2 & 0x7f
  530. if length_bits == 0x7e:
  531. length_data = self._recv_strict(2)
  532. self._frame_length = struct.unpack("!H", length_data)[0]
  533. elif length_bits == 0x7f:
  534. length_data = self._recv_strict(8)
  535. self._frame_length = struct.unpack("!Q", length_data)[0]
  536. else:
  537. self._frame_length = length_bits
  538. # Mask
  539. if self._frame_mask is None:
  540. self._frame_mask = self._recv_strict(4) if has_mask else ""
  541. # Payload
  542. payload = self._recv_strict(self._frame_length)
  543. if has_mask:
  544. payload = ABNF.mask(self._frame_mask, payload)
  545. # Reset for next frame
  546. self._frame_header = None
  547. self._frame_length = None
  548. self._frame_mask = None
  549. return ABNF(fin, rsv1, rsv2, rsv3, opcode, has_mask, payload)
  550. def send_close(self, status=STATUS_NORMAL, reason=""):
  551. """
  552. send close data to the server.
  553. status: status code to send. see STATUS_XXX.
  554. reason: the reason to close. This must be string.
  555. """
  556. if status < 0 or status >= ABNF.LENGTH_16:
  557. raise ValueError("code is invalid range")
  558. self.send(struct.pack('!H', status) + reason, ABNF.OPCODE_CLOSE)
  559. def close(self, status=STATUS_NORMAL, reason=""):
  560. """
  561. Close Websocket object
  562. status: status code to send. see STATUS_XXX.
  563. reason: the reason to close. This must be string.
  564. """
  565. if self.connected:
  566. if status < 0 or status >= ABNF.LENGTH_16:
  567. raise ValueError("code is invalid range")
  568. try:
  569. self.send(struct.pack('!H', status) + reason, ABNF.OPCODE_CLOSE)
  570. timeout = self.sock.gettimeout()
  571. self.sock.settimeout(3)
  572. try:
  573. frame = self.recv_frame()
  574. if logger.isEnabledFor(logging.ERROR):
  575. recv_status = struct.unpack("!H", frame.data)[0]
  576. if recv_status != STATUS_NORMAL:
  577. logger.error("close status: " + repr(recv_status))
  578. except:
  579. pass
  580. self.sock.settimeout(timeout)
  581. self.sock.shutdown(socket.SHUT_RDWR)
  582. except:
  583. pass
  584. self._closeInternal()
  585. def _closeInternal(self):
  586. self.connected = False
  587. self.sock.close()
  588. def _send(self, data):
  589. try:
  590. return self.sock.send(data)
  591. except socket.timeout as e:
  592. raise WebSocketTimeoutException(e.message)
  593. except Exception as e:
  594. if "timed out" in e.message:
  595. raise WebSocketTimeoutException(e.message)
  596. else:
  597. raise e
  598. def _recv(self, bufsize):
  599. try:
  600. bytes = self.sock.recv(bufsize)
  601. except socket.timeout as e:
  602. raise WebSocketTimeoutException(e.message)
  603. except SSLError as e:
  604. if e.message == "The read operation timed out":
  605. raise WebSocketTimeoutException(e.message)
  606. else:
  607. raise
  608. if not bytes:
  609. raise WebSocketConnectionClosedException()
  610. return bytes
  611. def _recv_strict(self, bufsize):
  612. shortage = bufsize - sum(len(x) for x in self._recv_buffer)
  613. while shortage > 0:
  614. bytes = self._recv(shortage)
  615. self._recv_buffer.append(bytes)
  616. shortage -= len(bytes)
  617. unified = "".join(self._recv_buffer)
  618. if shortage == 0:
  619. self._recv_buffer = []
  620. return unified
  621. else:
  622. self._recv_buffer = [unified[bufsize:]]
  623. return unified[:bufsize]
  624. def _recv_line(self):
  625. line = []
  626. while True:
  627. c = self._recv(1)
  628. line.append(c)
  629. if c == "\n":
  630. break
  631. return "".join(line)
  632. class WebSocketApp(object):
  633. """
  634. Higher level of APIs are provided.
  635. The interface is like JavaScript WebSocket object.
  636. """
  637. def __init__(self, url, header=[],
  638. on_open=None, on_message=None, on_error=None,
  639. on_close=None, keep_running=True, get_mask_key=None):
  640. """
  641. url: websocket url.
  642. header: custom header for websocket handshake.
  643. on_open: callable object which is called at opening websocket.
  644. this function has one argument. The arugment is this class object.
  645. on_message: callbale object which is called when recieved data.
  646. on_message has 2 arguments.
  647. The 1st arugment is this class object.
  648. The passing 2nd arugment is utf-8 string which we get from the server.
  649. on_error: callable object which is called when we get error.
  650. on_error has 2 arguments.
  651. The 1st arugment is this class object.
  652. The passing 2nd arugment is exception object.
  653. on_close: callable object which is called when closed the connection.
  654. this function has one argument. The arugment is this class object.
  655. keep_running: a boolean flag indicating whether the app's main loop should
  656. keep running, defaults to True
  657. get_mask_key: a callable to produce new mask keys, see the WebSocket.set_mask_key's
  658. docstring for more information
  659. """
  660. self.url = url
  661. self.header = header
  662. self.on_open = on_open
  663. self.on_message = on_message
  664. self.on_error = on_error
  665. self.on_close = on_close
  666. self.keep_running = keep_running
  667. self.get_mask_key = get_mask_key
  668. self.sock = None
  669. def send(self, data, opcode=ABNF.OPCODE_TEXT):
  670. """
  671. send message.
  672. data: message to send. If you set opcode to OPCODE_TEXT, data must be utf-8 string or unicode.
  673. opcode: operation code of data. default is OPCODE_TEXT.
  674. """
  675. if self.sock.send(data, opcode) == 0:
  676. raise WebSocketConnectionClosedException()
  677. def close(self):
  678. """
  679. close websocket connection.
  680. """
  681. self.keep_running = False
  682. self.sock.close()
  683. def _send_ping(self, interval):
  684. while True:
  685. for i in range(interval):
  686. time.sleep(1)
  687. if not self.keep_running:
  688. return
  689. self.sock.ping()
  690. def run_forever(self, sockopt=None, sslopt=None, ping_interval=0):
  691. """
  692. run event loop for WebSocket framework.
  693. This loop is infinite loop and is alive during websocket is available.
  694. sockopt: values for socket.setsockopt.
  695. sockopt must be tuple and each element is argument of sock.setscokopt.
  696. sslopt: ssl socket optional dict.
  697. ping_interval: automatically send "ping" command every specified period(second)
  698. if set to 0, not send automatically.
  699. """
  700. if sockopt is None:
  701. sockopt = []
  702. if sslopt is None:
  703. sslopt = {}
  704. if self.sock:
  705. raise WebSocketException("socket is already opened")
  706. thread = None
  707. try:
  708. self.sock = WebSocket(self.get_mask_key, sockopt=sockopt, sslopt=sslopt)
  709. self.sock.settimeout(default_timeout)
  710. self.sock.connect(self.url, header=self.header)
  711. self._callback(self.on_open)
  712. if ping_interval:
  713. thread = threading.Thread(target=self._send_ping, args=(ping_interval,))
  714. thread.setDaemon(True)
  715. thread.start()
  716. while self.keep_running:
  717. data = self.sock.recv()
  718. if data is None:
  719. break
  720. self._callback(self.on_message, data)
  721. except Exception, e:
  722. self._callback(self.on_error, e)
  723. finally:
  724. if thread:
  725. self.keep_running = False
  726. self.sock.close()
  727. self._callback(self.on_close)
  728. self.sock = None
  729. def _callback(self, callback, *args):
  730. if callback:
  731. try:
  732. callback(self, *args)
  733. except Exception, e:
  734. logger.error(e)
  735. if logger.isEnabledFor(logging.DEBUG):
  736. _, _, tb = sys.exc_info()
  737. traceback.print_tb(tb)
  738. if __name__ == "__main__":
  739. enableTrace(True)
  740. ws = create_connection("ws://echo.websocket.org/")
  741. print("Sending 'Hello, World'...")
  742. ws.send("Hello, World")
  743. print("Sent")
  744. print("Receiving...")
  745. result = ws.recv()
  746. print("Received '%s'" % result)
  747. ws.close()