Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def _read_write(self, soc, max_idling=20):
- iw = [self.connection, soc]
- ow = []
- count = 0
- while 1:
- count += 1
- (ins, _, exs) = select.select(iw, ow, iw, 3)
- if exs: break
- if ins:
- for i in ins:
- if i is soc:
- out = self.connection
- else:
- out = soc
- data = i.recv(8192)
- if data:
- if 'image/png' in data:
- #for s in self.headers:
- # print s + ": " + self.headers[s]
- global imagenum
- fp = open("/home/tom/proxytest/" + str(imagenum) + ".png", "wb")
- imagenum = imagenum + 1
- pos = data.find("Content-Length: ");
- if(pos > 0):
- sz = data[pos+16:data.find('\n', pos)-1]#+16 to account for "content-length: ", -1 to account for \r
- print "Image size is: " + sz
- print "Writing image (test " + str(imagenum) + ") ... \n"
- image_start = data.find("\r\n\r\n")+4
- print
- if(int(sz) > len(data) - image_start): #if reported data size is greater than the amount of already-fetched data minus the header
- fp.write(data[image_start:])
- total = len(data[image_start:])
- while(total < int(sz)):
- global count
- out.send(data)
- count = 0
- data = i.recv(8192)
- fp.write(data)
- total = total + len(data)
- else:
- fp.write(data[image_start:])
- fp.close()
- else: #data is not in the response, and we don't know the size
- #here data contains just the header, so send that and read more before writing to file
- while True:
- global count
- out.send(data)
- count = 0
- data = i.recv(8192)
- fp.write(data)
- if(len(data) != 8192):
- break
- fp.close()
- out.send(data)
- count = 0
- else:
- print "\t" "idle", count
- if count == max_idling: break
Add Comment
Please, Sign In to add comment