Guest User

Untitled

a guest
Oct 22nd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. def _read_write(self, soc, max_idling=20):
  2. iw = [self.connection, soc]
  3. ow = []
  4. count = 0
  5. while 1:
  6. count += 1
  7. (ins, _, exs) = select.select(iw, ow, iw, 3)
  8. if exs: break
  9. if ins:
  10. for i in ins:
  11. if i is soc:
  12. out = self.connection
  13. else:
  14. out = soc
  15. data = i.recv(8192)
  16. if data:
  17. if 'image/png' in data:
  18. #for s in self.headers:
  19. # print s + ": " + self.headers[s]
  20. global imagenum
  21. fp = open("/home/tom/proxytest/" + str(imagenum) + ".png", "wb")
  22. imagenum = imagenum + 1
  23. pos = data.find("Content-Length: ");
  24. if(pos > 0):
  25. sz = data[pos+16:data.find('\n', pos)-1]#+16 to account for "content-length: ", -1 to account for \r
  26. print "Image size is: " + sz
  27. print "Writing image (test " + str(imagenum) + ") ... \n"
  28. image_start = data.find("\r\n\r\n")+4
  29. print
  30. if(int(sz) > len(data) - image_start): #if reported data size is greater than the amount of already-fetched data minus the header
  31. fp.write(data[image_start:])
  32. total = len(data[image_start:])
  33. while(total < int(sz)):
  34. global count
  35. out.send(data)
  36. count = 0
  37. data = i.recv(8192)
  38. fp.write(data)
  39. total = total + len(data)
  40. else:
  41. fp.write(data[image_start:])
  42. fp.close()
  43. else: #data is not in the response, and we don't know the size
  44. #here data contains just the header, so send that and read more before writing to file
  45. while True:
  46. global count
  47. out.send(data)
  48. count = 0
  49. data = i.recv(8192)
  50. fp.write(data)
  51. if(len(data) != 8192):
  52. break
  53. fp.close()
  54.  
  55.  
  56. out.send(data)
  57. count = 0
  58. else:
  59. print "\t" "idle", count
  60. if count == max_idling: break
Add Comment
Please, Sign In to add comment