websocket.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import socket
  2. import ssl
  3. from frame import receive_frame, pop_frame, contains_frame
  4. from handshake import ServerHandshake, ClientHandshake
  5. from errors import SSLError
  6. INHERITED_ATTRS = ['bind', 'close', 'listen', 'fileno', 'getpeername',
  7. 'getsockname', 'getsockopt', 'setsockopt', 'setblocking',
  8. 'settimeout', 'gettimeout', 'shutdown', 'family', 'type',
  9. 'proto']
  10. class websocket(object):
  11. """
  12. Implementation of web socket, upgrades a regular TCP socket to a websocket
  13. using the HTTP handshakes and frame (un)packing, as specified by RFC 6455.
  14. The API of a websocket is identical to that of a regular socket, as
  15. illustrated by the examples below.
  16. Server example:
  17. >>> import wspy, socket
  18. >>> sock = wspy.websocket()
  19. >>> sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  20. >>> sock.bind(('', 8000))
  21. >>> sock.listen(5)
  22. >>> client = sock.accept()
  23. >>> client.send(wspy.Frame(wspy.OPCODE_TEXT, 'Hello, Client!'))
  24. >>> frame = client.recv()
  25. Client example:
  26. >>> import wspy
  27. >>> sock = wspy.websocket(location='/my/path')
  28. >>> sock.connect(('', 8000))
  29. >>> sock.send(wspy.Frame(wspy.OPCODE_TEXT, 'Hello, Server!'))
  30. """
  31. def __init__(self, sock=None, protocols=[], extensions=[], origin=None,
  32. location='/', trusted_origins=[], locations=[], auth=None,
  33. recv_callback=None, sfamily=socket.AF_INET, sproto=0):
  34. """
  35. Create a regular TCP socket of family `family` and protocol
  36. `sock` is an optional regular TCP socket to be used for sending binary
  37. data. If not specified, a new socket is created.
  38. `protocols` is a list of supported protocol names.
  39. `extensions` is a list of supported extensions (`Extension` instances).
  40. `origin` (for client sockets) is the value for the "Origin" header sent
  41. in a client handshake .
  42. `location` (for client sockets) is optional, used to request a
  43. particular resource in the HTTP handshake. In a URL, this would show as
  44. ws://host[:port]/<location>. Use this when the server serves multiple
  45. resources (see `locations`).
  46. `trusted_origins` (for server sockets) is a list of expected values
  47. for the "Origin" header sent by a client. If the received Origin header
  48. has value not in this list, a HandshakeError is raised. If the list is
  49. empty (default), all origins are excepted.
  50. `locations` (for server sockets) is an optional list of resources
  51. serverd by this server. If specified (without trailing slashes), these
  52. are used to verify the resource location requested by a client. The
  53. requested location may be used to distinquish different services in a
  54. server implementation.
  55. `auth` is optional, used for HTTP Basic or Digest authentication during
  56. the handshake. It must be specified as a (username, password) tuple.
  57. `recv_callback` is the callback for received frames in asynchronous
  58. sockets. Use in conjunction with setblocking(0). The callback itself
  59. may for example change the recv_callback attribute to change the
  60. behaviour for the next received message. Can be set when calling
  61. `queue_send`.
  62. `sfamily` and `sproto` are used for the regular socket constructor.
  63. """
  64. self.protocols = protocols
  65. self.extensions = extensions
  66. self.origin = origin
  67. self.location = location
  68. self.trusted_origins = trusted_origins
  69. self.locations = locations
  70. self.auth = auth
  71. self.secure = False
  72. self.handshake_sent = False
  73. self.hooks_send = []
  74. self.hooks_recv = []
  75. self.sendbuf_frames = []
  76. self.sendbuf = ''
  77. self.recvbuf = ''
  78. self.recv_callback = recv_callback
  79. self.sock = sock or socket.socket(sfamily, socket.SOCK_STREAM, sproto)
  80. def __getattr__(self, name):
  81. if name in INHERITED_ATTRS:
  82. return getattr(self.sock, name)
  83. raise AttributeError("'%s' has no attribute '%s'"
  84. % (self.__class__.__name__, name))
  85. def accept(self):
  86. """
  87. Equivalent to socket.accept(), but transforms the socket into a
  88. websocket instance and sends a server handshake (after receiving a
  89. client handshake). Note that the handshake may raise a HandshakeError
  90. exception.
  91. """
  92. sock, address = self.sock.accept()
  93. wsock = websocket(sock)
  94. wsock.secure = self.secure
  95. ServerHandshake(wsock).perform(self)
  96. wsock.handshake_sent = True
  97. return wsock, address
  98. def connect(self, address):
  99. """
  100. Equivalent to socket.connect(), but sends an client handshake request
  101. after connecting.
  102. `address` is a (host, port) tuple of the server to connect to.
  103. """
  104. self.sock.connect(address)
  105. ClientHandshake(self).perform()
  106. self.handshake_sent = True
  107. def send(self, *args):
  108. """
  109. Send a number of frames.
  110. """
  111. for frame in args:
  112. for hook in self.hooks_send:
  113. frame = hook(frame)
  114. #print 'send frame:', frame, 'to %s:%d' % self.sock.getpeername()
  115. self.sock.sendall(frame.pack())
  116. def recv(self):
  117. """
  118. Receive a single frames. This can be either a data frame or a control
  119. frame.
  120. """
  121. frame = receive_frame(self.sock)
  122. for hook in self.hooks_recv:
  123. frame = hook(frame)
  124. #print 'receive frame:', frame, 'from %s:%d' % self.sock.getpeername()
  125. return frame
  126. def recvn(self, n):
  127. """
  128. Receive exactly `n` frames. These can be either data frames or control
  129. frames, or a combination of both.
  130. """
  131. return [self.recv() for i in xrange(n)]
  132. def queue_send(self, frame, callback=None, recv_callback=None):
  133. """
  134. Enqueue `frame` to the send buffer so that it is send on the next
  135. `do_async_send`. `callback` is an optional callable to call when the
  136. frame has been fully written. `recv_callback` is an optional callable
  137. to quickly set the `recv_callback` attribute to.
  138. """
  139. for hook in self.hooks_send:
  140. frame = hook(frame)
  141. self.sendbuf += frame.pack()
  142. self.sendbuf_frames.append([frame, len(self.sendbuf), callback])
  143. if recv_callback:
  144. self.recv_callback = recv_callback
  145. def do_async_send(self):
  146. """
  147. Send any queued data.
  148. """
  149. assert len(self.sendbuf)
  150. nwritten = self.sock.send(self.sendbuf)
  151. nframes = 0
  152. for entry in self.sendbuf_frames:
  153. frame, offset, callback = entry
  154. if offset <= nwritten:
  155. nframes += 1
  156. if callback:
  157. print 'write cb'
  158. callback()
  159. else:
  160. entry[1] -= nwritten
  161. self.sendbuf = self.sendbuf[nwritten:]
  162. self.sendbuf_frames = self.sendbuf_frames[nframes:]
  163. def do_async_recv(self, bufsize):
  164. """
  165. """
  166. data = self.sock.recv(bufsize)
  167. if len(data) == 0:
  168. raise socket.error('no data to receive')
  169. self.recvbuf += data
  170. while contains_frame(self.recvbuf):
  171. frame, self.recvbuf = pop_frame(self.recvbuf)
  172. if not self.recv_callback:
  173. raise ValueError('no callback installed for %s' % frame)
  174. self.recv_callback(frame)
  175. def can_send(self):
  176. return len(self.sendbuf) > 0
  177. def can_recv(self):
  178. return self.recv_callback is not None
  179. def enable_ssl(self, *args, **kwargs):
  180. """
  181. Transforms the regular socket.socket to an ssl.SSLSocket for secure
  182. connections. Any arguments are passed to ssl.wrap_socket:
  183. http://docs.python.org/dev/library/ssl.html#ssl.wrap_socket
  184. """
  185. if self.handshake_sent:
  186. raise SSLError('can only enable SSL before handshake')
  187. self.secure = True
  188. self.sock = ssl.wrap_socket(self.sock, *args, **kwargs)
  189. def add_hook(self, send=None, recv=None, prepend=False):
  190. """
  191. Add a pair of send and receive hooks that are called for each frame
  192. that is sent or received. A hook is a function that receives a single
  193. argument - a Frame instance - and returns a `Frame` instance as well.
  194. `prepend` is a flag indicating whether the send hook is prepended to
  195. the other send hooks. This is expecially useful when a program uses
  196. extensions such as the built-in `DeflateFrame` extension. These
  197. extensions are installed using these hooks as well.
  198. For example, the following code creates a `Frame` instance for data
  199. being sent and removes the instance for received data. This way, data
  200. can be sent and received as if on a regular socket.
  201. >>> import wspy
  202. >>> sock = wspy.websocket()
  203. >>> sock.add_hook(lambda data: tswpy.Frame(tswpy.OPCODE_TEXT, data),
  204. >>> lambda frame: frame.payload)
  205. To add base64 encoding to the example above:
  206. >>> import base64
  207. >>> sock.add_hook(base64.encodestring, base64.decodestring, True)
  208. Note that here `prepend=True`, so that data passed to `send()` is first
  209. encoded and then packed into a frame. Of course, one could also decide
  210. to add the base64 hook first, or to return a new `Frame` instance with
  211. base64-encoded data.
  212. """
  213. if send:
  214. self.hooks_send.insert(0 if prepend else -1, send)
  215. if recv:
  216. self.hooks_recv.insert(-1 if prepend else 0, recv)