简体   繁体   中英

Python seek wfile like open?

please take a few time of you to help me. How can I use seek with wfile:

self.wfile = self.connection.makefile('wb', self.wbufsize)

My code look like this:

self.wfile.seek(offset, 0) self.wfile.write(r.data)

But problem is, my ILDE show this error every time I try to run my code: self.wfile.seek(offset, 0) io.UnsupportedOperation: seek

I thought wfile and open are the same, but why I cannot seek like open ? Even if it is true, I still think there is a way to bypass this restrict..

Note: If you at least one time hear about http.server or BaseHTTPServer you probably understood what wfile is.

EDIT: I edit my post to add my code, only a part of my full software, but this others part is not really needed:

        self.send_response(200)
        self.end_headers()

        def accelerator(url=None, splitBy=3):


            def buildRange(url, numsplits):
                global globaldownloadersave
                value = int(self.pool.urlopen('HEAD', url).headers["content-length"])
                print("Fullsize: ", value)
                print("Try devide with :", value / numsplits)
                lst = []
                for i in range(numsplits):
                    if i == range(numsplits):
                        lst.append('%s-%s' % (i * value//numsplits + 1, i * value//numsplits + 1 + (value - (i * value//numsplits + 1))))
                    if i == 0:
                        lst.append('%s-%s' % (0, value//numsplits))
                    else:
                        lst.append('%s-%s' % (i * value//numsplits + 1, (i + 1) * value//numsplits))
                return lst
            def downloadChunk(idx, irange):
                global globaldownloadersave
                r = self.pool.urlopen('GET', url, headers={'Range': 'bytes=' + str(irange)})
                offset = int(re.sub("(^.*?)-(.*?)$", "\\1", irange))
                offset2 = int(re.sub("(^.*?)-(.*?)$", "\\2", irange))
                self.wfile.seek(offset, 0)
                self.wfile.write(r.data)

            #self.data = io.BytesIO(b'')
            ranges = buildRange(url, splitBy)
            tasks = []
            # create one downloading thread per chunk
            #q = queue.Queue()  big fail, so comment it
            downloaders = [
                threading.Thread(
                    target=downloadChunk, 
                    args=(idx, irange),
                )
                for idx,irange in enumerate(ranges)
                ]

            # start threads, let run in parallel, wait for all to finish
            for th in downloaders:
                th.start()

            for th in downloaders:
                th.join()

        accelerator(self.url, 4)
        self.close_connection = 1
        return

A socket connection is a stream. Bytes once read from a stream are gone. So seek makes no sense. While it is possible to keep all read bytes in memory and simulate a seek , this is normally not preferred. Try to write your code without the need of seek .

EDIT: Didn't see it at first sight. You try to seek in a writing stream. This will be never possible, because you cannot say the receiving end "forget about all I've send, you get new data". If you really need that functionality you have to save the data locally in a normal file, and, when finished, send this file as one block to the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM