wsencoding.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/python
  2. '''
  3. WebSocket server-side load test program. Sends and receives traffic
  4. that has a random payload (length and content) that is checksummed and
  5. given a sequence number. Any errors are reported and counted.
  6. '''
  7. import sys, os, socket, ssl, time, traceback
  8. import random, time
  9. from base64 import b64encode, b64decode
  10. from codecs import utf_8_encode, utf_8_decode
  11. from select import select
  12. sys.path.insert(0,os.path.dirname(__file__) + "/../utils/")
  13. from websocket import *
  14. buffer_size = 65536
  15. recv_cnt = send_cnt = 0
  16. def check(buf):
  17. if buf[0] != '\x00' or buf[-1] != '\xff':
  18. raise Exception("Invalid WS packet")
  19. for decoded in decode(buf):
  20. nums = [ord(c) for c in decoded]
  21. print "Received nums: ", nums
  22. return
  23. def responder(client):
  24. cpartial = ""
  25. socks = [client]
  26. sent = False
  27. received = False
  28. while True:
  29. ins, outs, excepts = select(socks, socks, socks, 1)
  30. if excepts: raise Exception("Socket exception")
  31. if client in ins:
  32. buf = client.recv(buffer_size)
  33. if len(buf) == 0: raise Exception("Client closed")
  34. received = True
  35. #print "Client recv: %s (%d)" % (repr(buf[1:-1]), len(buf))
  36. if buf[-1] == '\xff':
  37. if cpartial:
  38. err = check(cpartial + buf)
  39. cpartial = ""
  40. else:
  41. err = check(buf)
  42. if err:
  43. print err
  44. else:
  45. print "received partitial"
  46. cpartial = cpartial + buf
  47. if received and not sent and client in outs:
  48. sent = True
  49. #nums = "".join([unichr(c) for c in range(0,256)])
  50. #nums = "".join([chr(c) for c in range(1,128)])
  51. #nums = nums + chr(194) + chr(128) + chr(194) + chr(129)
  52. #nums = "".join([chr(c) for c in range(0,256)])
  53. nums = "\x81\xff"
  54. nums = nums + "".join([chr(c) for c in range(0,256,4)])
  55. nums = nums + "\x00\x40\x41\xff\x81"
  56. # print nums
  57. client.send(encode(nums))
  58. # client.send("\x00" + nums + "\xff")
  59. # print "Sent characters 0-255"
  60. # #print "Client send: %s (%d)" % (repr(nums), len(nums))
  61. if __name__ == '__main__':
  62. try:
  63. if len(sys.argv) < 2: raise
  64. listen_port = int(sys.argv[1])
  65. except:
  66. print "Usage: <listen_port>"
  67. sys.exit(1)
  68. settings['listen_port'] = listen_port
  69. settings['daemon'] = False
  70. settings['handler'] = responder
  71. start_server()