Guest User

Untitled

a guest
Oct 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import struct
  2. import socket
  3. import random
  4.  
  5. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  6. client_socket.connect(('localhost', 1717))
  7.  
  8.  
  9. def tryRead() :
  10. flag = False
  11. readBannerBytes = 0
  12. bannerLength = 2
  13. readFrameBytes = 0
  14. frameBodyLengthRemaining = 0
  15. frameBody = ''
  16. banner = {
  17. 'version':0,
  18. 'length':0,
  19. 'pid':0,
  20. 'realWidth':0,
  21. 'realHeight':0,
  22. 'virtualWidth':0,
  23. 'virtualHeight':0,
  24. 'orientation':0,
  25. 'quirks':0
  26. }
  27. while True:
  28. chunk = client_socket.recv(4084)
  29. if len(chunk) == 0:
  30. continue
  31.  
  32. print('chunk(length=%d)' % len(chunk))
  33. cursor = 0
  34. while cursor < len(chunk):
  35.  
  36. if (readBannerBytes < bannerLength) :
  37. print(readBannerBytes,"---", bannerLength)
  38. if readBannerBytes == 0:
  39. banner['version'] = int(chunk[cursor].encode('hex'), 16)
  40. elif readBannerBytes == 1:
  41. banner['length'] = bannerLength = int(chunk[cursor].encode('hex'), 16)
  42. elif readBannerBytes >= 2 and readBannerBytes <= 5:
  43. banner['pid'] = int(chunk[cursor].encode('hex'), 16)
  44. elif readBannerBytes == 23:
  45. banner['quirks'] = int(chunk[cursor].encode('hex'), 16)
  46.  
  47. cursor += 1
  48. readBannerBytes += 1
  49.  
  50. if readBannerBytes == bannerLength:
  51. print('banner', banner)
  52.  
  53. elif readFrameBytes < 4:
  54. frameBodyLengthRemaining += (int(chunk[cursor].encode('hex'), 16) << (readFrameBytes * 8))
  55. frameBodyLengthRemaining_orig = frameBodyLengthRemaining
  56. cursor += 1
  57. readFrameBytes += 1
  58. print('headerbyte%d(val=%d)' %(readFrameBytes, frameBodyLengthRemaining))
  59.  
  60. else:
  61. # if this chunk has data of next image
  62. if len(chunk) - cursor >= frameBodyLengthRemaining:
  63. print('bodyfin(len=%d,cursor=%d)' % (frameBodyLengthRemaining, cursor))
  64. frameBody = frameBody + chunk[cursor:(cursor+frameBodyLengthRemaining)]
  65. if frameBody[0] != '\xff' or frameBody[1] != '\xd8':
  66. print("Frame body does not strt with JPEG header", frameBody[0], frameBody[1])
  67. exit()
  68. arr.append(frameBody)
  69. if len(arr) == 1:
  70. return 0
  71. cursor += frameBodyLengthRemaining
  72. frameBodyLengthRemaining = 0
  73. readFrameBytes = 0
  74. frameBody = ''
  75. else:
  76. # else this chunk is still for the current image
  77. print('body(len=%d)' % ( len(chunk) - cursor), 'remaining = %d' % frameBodyLengthRemaining)
  78. frameBody = frameBody + chunk[cursor:len(chunk)]
  79. frameBodyLengthRemaining -= (len(chunk) - cursor)
  80. readFrameBytes += len(chunk) - cursor
  81. cursor = len(chunk)
  82.  
  83.  
  84.  
  85. arr = []
  86. tryRead()
  87. for i in arr:
  88. with open("ss_"+str(random.randint(0, 10000))+".jpeg", "wb") as f:
  89. f.write(i)
Add Comment
Please, Sign In to add comment