OLD | NEW |
1 """ | 1 """ |
2 websocket - WebSocket client library for Python | 2 websocket - WebSocket client library for Python |
3 | 3 |
4 Copyright (C) 2010 Hiroki Ohtani(liris) | 4 Copyright (C) 2010 Hiroki Ohtani(liris) |
5 | 5 |
6 This library is free software; you can redistribute it and/or | 6 This library is free software; you can redistribute it and/or |
7 modify it under the terms of the GNU Lesser General Public | 7 modify it under the terms of the GNU Lesser General Public |
8 License as published by the Free Software Foundation; either | 8 License as published by the Free Software Foundation; either |
9 version 2.1 of the License, or (at your option) any later version. | 9 version 2.1 of the License, or (at your option) any later version. |
10 | 10 |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 if is_secure: | 445 if is_secure: |
446 if HAVE_SSL: | 446 if HAVE_SSL: |
447 if self.sslopt is None: | 447 if self.sslopt is None: |
448 sslopt = {} | 448 sslopt = {} |
449 else: | 449 else: |
450 sslopt = self.sslopt | 450 sslopt = self.sslopt |
451 self.sock = ssl.wrap_socket(self.sock, **sslopt) | 451 self.sock = ssl.wrap_socket(self.sock, **sslopt) |
452 else: | 452 else: |
453 raise WebSocketException("SSL not available.") | 453 raise WebSocketException("SSL not available.") |
454 | 454 |
| 455 print "STARTING HANDSHAKE: %s" % time.time() |
| 456 print " hostname: %s" % hostname |
| 457 print " port: %s" % port |
| 458 print " START TRACEBACK" |
| 459 import traceback; traceback.print_stack() |
| 460 print "END TRACEBACK" |
455 self._handshake(hostname, port, resource, **options) | 461 self._handshake(hostname, port, resource, **options) |
456 | 462 |
457 def _handshake(self, host, port, resource, **options): | 463 def _handshake(self, host, port, resource, **options): |
458 sock = self.sock | 464 sock = self.sock |
459 headers = [] | 465 headers = [] |
460 headers.append("GET %s HTTP/1.1" % resource) | 466 headers.append("GET %s HTTP/1.1" % resource) |
461 headers.append("Upgrade: websocket") | 467 headers.append("Upgrade: websocket") |
462 headers.append("Connection: Upgrade") | 468 headers.append("Connection: Upgrade") |
463 if port == 80: | 469 if port == 80: |
464 hostport = host | 470 hostport = host |
(...skipping 17 matching lines...) Expand all Loading... |
482 | 488 |
483 header_str = "\r\n".join(headers) | 489 header_str = "\r\n".join(headers) |
484 self._send(header_str) | 490 self._send(header_str) |
485 if traceEnabled: | 491 if traceEnabled: |
486 logger.debug("--- request header ---") | 492 logger.debug("--- request header ---") |
487 logger.debug(header_str) | 493 logger.debug(header_str) |
488 logger.debug("-----------------------") | 494 logger.debug("-----------------------") |
489 | 495 |
490 status, resp_headers = self._read_headers() | 496 status, resp_headers = self._read_headers() |
491 if status != 101: | 497 if status != 101: |
| 498 print "FAILED HANDSHAKE: %s" % time.time() |
492 self.close() | 499 self.close() |
493 raise WebSocketException("Handshake Status %d" % status) | 500 raise WebSocketException("Handshake Status %d" % status) |
494 | 501 |
495 success = self._validate_header(resp_headers, key) | 502 success = self._validate_header(resp_headers, key) |
496 if not success: | 503 if not success: |
497 self.close() | 504 self.close() |
498 raise WebSocketException("Invalid WebSocket Header") | 505 raise WebSocketException("Invalid WebSocket Header") |
499 | 506 |
| 507 print "HANDSHAKE SUCCEEDED" |
500 self.connected = True | 508 self.connected = True |
501 | 509 |
502 def _validate_header(self, headers, key): | 510 def _validate_header(self, headers, key): |
503 for k, v in _HEADERS_TO_CHECK.iteritems(): | 511 for k, v in _HEADERS_TO_CHECK.iteritems(): |
504 r = headers.get(k, None) | 512 r = headers.get(k, None) |
505 if not r: | 513 if not r: |
506 return False | 514 return False |
507 r = r.lower() | 515 r = r.lower() |
508 if v != r: | 516 if v != r: |
509 return False | 517 return False |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 if __name__ == "__main__": | 892 if __name__ == "__main__": |
885 enableTrace(True) | 893 enableTrace(True) |
886 ws = create_connection("ws://echo.websocket.org/") | 894 ws = create_connection("ws://echo.websocket.org/") |
887 print("Sending 'Hello, World'...") | 895 print("Sending 'Hello, World'...") |
888 ws.send("Hello, World") | 896 ws.send("Hello, World") |
889 print("Sent") | 897 print("Sent") |
890 print("Receiving...") | 898 print("Receiving...") |
891 result = ws.recv() | 899 result = ws.recv() |
892 print("Received '%s'" % result) | 900 print("Received '%s'" % result) |
893 ws.close() | 901 ws.close() |
OLD | NEW |