Advertisement
Guest User

websocket_client.py

a guest
Mar 6th, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.70 KB | None | 0 0
  1. """
  2. websocket - WebSocket client library for Python
  3.  
  4. Copyright (C) 2010 Hiroki Ohtani(liris)
  5.  
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10.  
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15.  
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19.  
  20. """
  21.  
  22. # molecular got this from http://pypi.python.org/pypi/websocket-client/0.4
  23. __all__ = ["create_connection"]
  24.  
  25. import socket
  26. from urlparse import urlparse
  27. import random
  28. import struct
  29. import md5
  30. import logging
  31.  
  32.  
  33. logger = logging.getLogger()
  34.  
  35. class WebSocketException(Exception):
  36. pass
  37.  
  38. class ConnectionClosedException(WebSocketException):
  39. pass
  40.  
  41. default_timeout = None
  42. traceEnabled = False
  43.  
  44. def enableTrace(tracable):
  45. """
  46. turn on/off the tracability.
  47. """
  48. global traceEnabled
  49. traceEnabled = tracable
  50. if tracable:
  51. if not logger.handlers:
  52. logger.addHandler(logging.StreamHandler())
  53. logger.setLevel(logging.DEBUG)
  54.  
  55. def setdefaulttimeout(timeout):
  56. """
  57. Set the global timeout setting to connect.
  58. """
  59. global default_timeout
  60. default_timeout = timeout
  61.  
  62.  
  63. def getdefaulttimeout():
  64. """
  65. Return the global timeout setting to connect.
  66. """
  67. return default_timeout
  68.  
  69. def _parse_url(url):
  70. """
  71. parse url and the result is tuple of
  72. (hostname, port, resource path and the flag of secure mode)
  73. """
  74. parsed = urlparse(url)
  75. if parsed.hostname:
  76. hostname = parsed.hostname
  77. else:
  78. raise ValueError("hostname is invalid")
  79. port = 0
  80. if parsed.port:
  81. port = parsed.port
  82.  
  83. is_secure = False
  84. if parsed.scheme == "ws":
  85. if not port:
  86. port = 80
  87. elif parsed.scheme == "wss":
  88. is_secure = True
  89. if not port:
  90. port = 443
  91. else:
  92. raise ValueError("scheme %s is invalid" % parsed.scheme)
  93.  
  94. if parsed.path:
  95. resource = parsed.path
  96. else:
  97. resource = "/"
  98.  
  99. return (hostname, port, resource, is_secure)
  100.  
  101.  
  102. def create_connection(url, timeout=None, **options):
  103. """
  104. connect to url and return websocket object.
  105.  
  106. Connect to url and return the WebSocket object.
  107. Passing optional timeout parameter will set the timeout on the socket.
  108. If no timeout is supplied, the global default timeout setting returned by getdefauttimeout() is used.
  109. """
  110. websock = WebSocket()
  111. websock.settimeout(timeout != None and timeout or default_timeout)
  112. websock.connect(url, **options)
  113. return websock
  114.  
  115. _MAX_INTEGER = (1 << 32) -1
  116. _AVAILABLE_KEY_CHARS = range(0x21, 0x2f + 1) + range(0x3a, 0x7e + 1)
  117. _MAX_CHAR_BYTE = (1<<8) -1
  118.  
  119. # ref. Websocket gets an update, and it breaks stuff.
  120. # http://axod.blogspot.com/2010/06/websocket-gets-update-and-it-breaks.html
  121.  
  122. def _create_sec_websocket_key():
  123. spaces_n = random.randint(1, 12)
  124. max_n = _MAX_INTEGER / spaces_n
  125. number_n = random.randint(0, max_n)
  126. product_n = number_n * spaces_n
  127. key_n = str(product_n)
  128. for i in range(random.randint(1, 12)):
  129. c = random.choice(_AVAILABLE_KEY_CHARS)
  130. pos = random.randint(0, len(key_n))
  131. key_n = key_n[0:pos] + chr(c) + key_n[pos:]
  132. for i in range(spaces_n):
  133. pos = random.randint(1, len(key_n)-1)
  134. key_n = key_n[0:pos] + " " + key_n[pos:]
  135.  
  136. return number_n, key_n
  137.  
  138. def _create_key3():
  139. return "".join([chr(random.randint(0, _MAX_CHAR_BYTE)) for i in range(8)])
  140.  
  141. HEADERS_TO_CHECK = {
  142. "upgrade": "websocket",
  143. "connection": "upgrade",
  144. }
  145.  
  146. HEADERS_TO_EXIST_FOR_HYBI00 = [
  147. "sec-websocket-origin",
  148. "sec-websocket-location",
  149. ]
  150.  
  151. HEADERS_TO_EXIST_FOR_HIXIE75 = [
  152. "websocket-origin",
  153. "websocket-location",
  154. ]
  155.  
  156. class _SSLSocketWrapper(object):
  157. def __init__(self, sock):
  158. self.ssl = socket.ssl(sock)
  159.  
  160. def recv(self, bufsize):
  161. return self.ssl.read(bufsize)
  162.  
  163. def send(self, payload):
  164. return self.ssl.write(payload)
  165.  
  166. class WebSocket(object):
  167. """
  168. Low level WebSocket interface.
  169. This class is based on
  170. The WebSocket protocol draft-hixie-thewebsocketprotocol-76
  171. http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
  172.  
  173. We can connect to the websocket server and send/recieve data.
  174. The following example is a echo client.
  175.  
  176. >>> import websocket
  177. >>> ws = websocket.WebSocket()
  178. >>> ws.Connect("ws://localhost:8080/echo")
  179. >>> ws.send("Hello, Server")
  180. >>> ws.recv()
  181. 'Hello, Server'
  182. >>> ws.close()
  183. """
  184. def __init__(self):
  185. """
  186. Initalize WebSocket object.
  187. """
  188. self.connected = False
  189. self.io_sock = self.sock = socket.socket()
  190.  
  191. def settimeout(self, timeout):
  192. """
  193. Set the timeout to the websocket.
  194. """
  195. self.sock.settimeout(timeout)
  196.  
  197. def gettimeout(self):
  198. """
  199. Get the websocket timeout.
  200. """
  201. return self.sock.gettimeout()
  202.  
  203. def connect(self, url, **options):
  204. """
  205. Connect to url. url is websocket url scheme. ie. ws://host:port/resource
  206. """
  207. hostname, port, resource, is_secure = _parse_url(url)
  208. # TODO: we need to support proxy
  209. self.sock.connect((hostname, port))
  210. if is_secure:
  211. self.io_sock = _SSLSocketWrapper(self.sock)
  212. self._handshake(hostname, port, resource, **options)
  213.  
  214. def _handshake(self, host, port, resource, **options):
  215. sock = self.io_sock
  216. headers = []
  217. if "header" in options:
  218. headers.extend(options["header"])
  219.  
  220. headers.append("GET %s HTTP/1.1" % resource)
  221. headers.append("Upgrade: WebSocket")
  222. headers.append("Connection: Upgrade")
  223. if port == 80:
  224. hostport = host
  225. else:
  226. hostport = "%s:%d" % (host, port)
  227. headers.append("Host: %s" % hostport)
  228. headers.append("Origin: %s" % hostport)
  229.  
  230. number_1, key_1 = _create_sec_websocket_key()
  231. headers.append("Sec-WebSocket-Key1: %s" % key_1)
  232. number_2, key_2 = _create_sec_websocket_key()
  233. headers.append("Sec-WebSocket-Key2: %s" % key_2)
  234. headers.append("")
  235. key3 = _create_key3()
  236. headers.append(key3)
  237.  
  238. header_str = "\r\n".join(headers)
  239. sock.send(header_str)
  240. if traceEnabled:
  241. logger.debug( "--- request header ---")
  242. logger.debug( header_str)
  243. logger.debug("-----------------------")
  244.  
  245. status, resp_headers = self._read_headers()
  246. if status != 101:
  247. self.close()
  248. raise WebSocketException("Handshake Status %d" % status)
  249. success, secure = self._validate_header(resp_headers)
  250. if not success:
  251. self.close()
  252. raise WebSocketException("Invalid WebSocket Header")
  253.  
  254. if secure:
  255. resp = self._get_resp()
  256. if not self._validate_resp(number_1, number_2, key3, resp):
  257. self.close()
  258. raise WebSocketException("challenge-response error")
  259.  
  260. self.connected = True
  261.  
  262. def _validate_resp(self, number_1, number_2, key3, resp):
  263. challenge = struct.pack("!I", number_1)
  264. challenge += struct.pack("!I", number_2)
  265. challenge += key3
  266. digest = md5.md5(challenge).digest()
  267.  
  268. return resp == digest
  269.  
  270. def _get_resp(self):
  271. result = self._recv(16)
  272. if traceEnabled:
  273. logger.debug("--- challenge response result ---")
  274. logger.debug(repr(result))
  275. logger.debug("---------------------------------")
  276.  
  277. return result
  278.  
  279. def _validate_header(self, headers):
  280. #TODO: check other headers
  281. for key, value in HEADERS_TO_CHECK.iteritems():
  282. v = headers.get(key, None)
  283. if value != v:
  284. return False, False
  285.  
  286. success = 0
  287. for key in HEADERS_TO_EXIST_FOR_HYBI00:
  288. if key in headers:
  289. success += 1
  290. if success == len(HEADERS_TO_EXIST_FOR_HYBI00):
  291. return True, True
  292. elif success != 0:
  293. return False, True
  294.  
  295. success = 0
  296. for key in HEADERS_TO_EXIST_FOR_HIXIE75:
  297. if key in headers:
  298. success += 1
  299. if success == len(HEADERS_TO_EXIST_FOR_HIXIE75):
  300. return True, False
  301.  
  302. return False, False
  303.  
  304.  
  305. def _read_headers(self):
  306. status = None
  307. headers = {}
  308. if traceEnabled:
  309. logger.debug("--- response header ---")
  310.  
  311. while True:
  312. line = self._recv_line()
  313. if line == "\r\n":
  314. break
  315. line = line.strip()
  316. if traceEnabled:
  317. logger.debug(line)
  318. if not status:
  319. status_info = line.split(" ", 2)
  320. status = int(status_info[1])
  321. else:
  322. kv = line.split(":", 1)
  323. if len(kv) == 2:
  324. key, value = kv
  325. headers[key.lower()] = value.strip().lower()
  326. else:
  327. raise WebSocketException("Invalid header")
  328.  
  329. if traceEnabled:
  330. logger.debug("-----------------------")
  331.  
  332. return status, headers
  333.  
  334. def send(self, payload):
  335. """
  336. Send the data as string. payload must be utf-8 string or unicoce.
  337. """
  338. if isinstance(payload, unicode):
  339. payload = payload.encode("utf-8")
  340. data = "".join(["\x00", payload, "\xff"])
  341. self.io_sock.send(data)
  342. if traceEnabled:
  343. logger.debug("send: " + repr(data))
  344.  
  345. def recv(self):
  346. """
  347. Reeive utf-8 string data from the server.
  348. """
  349. b = self._recv(1)
  350. if enableTrace:
  351. logger.debug("recv frame: " + repr(b))
  352. frame_type = ord(b)
  353. if frame_type == 0x00:
  354. bytes = []
  355. while True:
  356. b = self._recv(1)
  357. if b == "\xff":
  358. break
  359. else:
  360. bytes.append(b)
  361. return "".join(bytes)
  362. elif 0x80 < frame_type < 0xff:
  363. # which frame type is valid?
  364. length = self._read_length()
  365. bytes = self._recv_strict(length)
  366. return bytes
  367. elif frame_type == 0xff:
  368. n = self._recv(1)
  369. self._closeInternal()
  370. return None
  371. else:
  372. raise WebSocketException("Invalid frame type")
  373.  
  374. def _read_length(self):
  375. length = 0
  376. while True:
  377. b = ord(self._recv(1))
  378. length = length * (1 << 7) + (b & 0x7f)
  379. if b < 0x80:
  380. break
  381.  
  382. return length
  383.  
  384. def close(self):
  385. """
  386. Close Websocket object
  387. """
  388. if self.connected:
  389. try:
  390. self.io_sock.send("\xff\x00")
  391. timeout = self.sock.gettimeout()
  392. self.sock.settimeout(1)
  393. try:
  394. result = self._recv(2)
  395. if result != "\xff\x00":
  396. logger.error("bad closing Handshake")
  397. except:
  398. pass
  399. self.sock.settimeout(timeout)
  400. self.sock.shutdown(socket.SHUT_RDWR)
  401. except:
  402. pass
  403. self._closeInternal()
  404.  
  405. def _closeInternal(self):
  406. self.connected = False
  407. self.sock.close()
  408. self.io_sock = self.sock
  409.  
  410. def _recv(self, bufsize):
  411. bytes = self.io_sock.recv(bufsize)
  412. if not bytes:
  413. raise ConnectionClosedException()
  414. return bytes
  415.  
  416. def _recv_strict(self, bufsize):
  417. remaining = bufsize
  418. bytes = ""
  419. while remaining:
  420. bytes += self._recv(remaining)
  421. remaining = bufsize - len(bytes)
  422.  
  423. return bytes
  424.  
  425. def _recv_line(self):
  426. line = []
  427. while True:
  428. c = self._recv(1)
  429. line.append(c)
  430. if c == "\n":
  431. break
  432. return "".join(line)
  433.  
  434. class WebSocketApp(object):
  435. """
  436. Higher level of APIs are provided.
  437. The interface is like JavaScript WebSocket object.
  438. """
  439. def __init__(self, url,
  440. on_open = None, on_message = None, on_error = None,
  441. on_close = None):
  442. """
  443. url: websocket url.
  444. on_open: callable object which is called at opening websocket.
  445. this function has one argument. The arugment is this class object.
  446. on_message: callbale object which is called when recieved data.
  447. on_message has 2 arguments.
  448. The 1st arugment is this class object.
  449. The passing 2nd arugment is utf-8 string which we get from the server.
  450. on_error: callable object which is called when we get error.
  451. on_error has 2 arguments.
  452. The 1st arugment is this class object.
  453. The passing 2nd arugment is exception object.
  454. on_close: callable object which is called when closed the connection.
  455. this function has one argument. The arugment is this class object.
  456. """
  457. self.url = url
  458. self.on_open = on_open
  459. self.on_message = on_message
  460. self.on_error = on_error
  461. self.on_close = on_close
  462. self.sock = None
  463.  
  464. def send(self, data):
  465. """
  466. send message. data must be utf-8 string or unicode.
  467. """
  468. self.sock.send(data)
  469.  
  470. def close(self):
  471. """
  472. close websocket connection.
  473. """
  474. self.sock.close()
  475.  
  476. def run_forever(self):
  477. """
  478. run event loop for WebSocket framework.
  479. This loop is infinite loop and is alive during websocket is available.
  480. """
  481. if self.sock:
  482. raise WebSocketException("socket is already opened")
  483. try:
  484. self.sock = WebSocket()
  485. self.sock.connect(self.url)
  486. self._run_with_no_err(self.on_open)
  487. while True:
  488. data = self.sock.recv()
  489. if data is None:
  490. break
  491. self._run_with_no_err(self.on_message, data)
  492. except Exception, e:
  493. self._run_with_no_err(self.on_error, e)
  494. finally:
  495. self.sock.close()
  496. self._run_with_no_err(self.on_close)
  497. self.sock = None
  498.  
  499. def _run_with_no_err(self, callback, *args):
  500. if callback:
  501. try:
  502. callback(self, *args)
  503. except Exception, e:
  504. if logger.isEnabledFor(logging.DEBUG):
  505. logger.error(e)
  506.  
  507.  
  508. if __name__ == "__main__":
  509. enableTrace(True)
  510. #ws = create_connection("ws://localhost:8080/echo")
  511. ws = create_connection("ws://localhost:5000/chat")
  512. print "Sending 'Hello, World'..."
  513. ws.send("Hello, World")
  514. print "Sent"
  515. print "Receiving..."
  516. result = ws.recv()
  517. print "Received '%s'" % result
  518. ws.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement