How does the file.seek() work ?

gert gert.cuykens at gmail.com
Thu Aug 27 21:18:28 CEST 2009


On Aug 26, 7:28 pm, gert <gert.cuyk... at gmail.com> wrote:
> On Aug 26, 12:46 am, Graham Dumpleton <graham.dumple... at gmail.com>
> wrote:
>
>
>
> > On Aug 25, 5:37 am, Tim Chase <python.l... at tim.thechases.com> wrote:
>
> > > > I want the file pointer set to 100 and overwrite everything from there
> > > [snip]
> > > > def application(environ, response):
> > > >     query=os.path.join(os.path.dirname(__file__),'teeeeeeeeeemp')
> > > >     range=environ.get('HTTP_RANGE','bytes=0-').replace
> > > > ('bytes=','').split(',')
> > > >     offset=[]
> > > >     for r in range: offset.append(r.split('-'))
> > > >     with open(query,'w+') as f:
> > > >          f.seek(int(offset[0][0]))
> > > >          while True:
> > > >              chunk=environ['wsgi.input'].read(8192).decode('latin1')
> > > >              if not chunk: break
> > > >              f.write(chunk)
> > > >     f=open(query)
> > > >     l=str(os.fstat(f.fileno()).st_size)
> > > >     response('200 OK', [('Content-Type', 'text/plain'), ('Content-
> > > > Length', str(len(l)))])
> > > >     return [l]
>
> > > A couple items of note:
>
> > > - you don't open the file in binary mode -- seek is more reliable
> > > in binary mode :)
>
> > If my memory is right, if file is opened in binary mode, also wouldn't
> > need to be decoding the WSGI input stream as latin-1 to get a string.
> > Instead can just deal with bytes and write bytes to file.
>
> > Graham
>
> > > - if you want to lop off the rest of the file, use f.truncate()
>
> > > An example:
>
> > > # create the initial file
> > >  >>> f = file('zzz.zzz', 'wb+')
> > >  >>> f.write('abcdefghijklmnop')
> > >  >>> f.close()
>
> > >  >>> f = file('zzz.zzz', 'ab+')
> > >  >>> f.read() # show the existing content
> > > 'abcdefghijklmnop'
> > >  >>> f.seek(5) # seek to the desired offset
> > >  >>> f.truncate() # throw away everything after here
> > >  >>> f.write('zyx') # write the new data at pos=5
> > >  >>> f.close()
>
> > > # demonstrate that it worked
> > >  >>> f = file('zzz.zzz', 'rb')
> > >  >>> f.read()
> > > 'abcdezyx'
> > >  >>> f.close()
>
> > > > also why must I open the file a second time to know how big it is ?
>
> > > Likely the output has been buffered.  You can try using
>
> > >    f.flush() # write all the data to the disk first
> > >    size = os.fstat(f.fileno()).st_size
>
> > > which seems to do the trick for me.
> > > -tkc
>
> Works thanks
>
> curl -C 10 -T upload2.wsgihttp://192.168.2.17/appwsgi/wsgi/upload2.wsgi
> --header "Transfer-Encoding: chunked" -v
>
> import os
>
> def application(environ, response):
>     #query=environ.get['QUERY_STRING']
>     #print (query, file=environ['wsgi.errors'])
>     query=os.path.join(os.path.dirname(__file__),'teeeeeeeeeemp')
>     range=environ.get('HTTP_CONTENT_RANGE','bytes 0-').replace('bytes
> ','').split('/')[0].split(',')
>     offset=[]
>     for r in range: offset.append(r.split('-'))
>     with open(query,'rb+') as f:
>          f.seek(int(offset[0][0]))
>          if environ['REQUEST_METHOD']=='PUT':
>              f.truncate()
>              while True:
>                  chunk=environ['wsgi.input'].read(8192)
>                  if not chunk: break
>                  f.write(chunk)
>          f.flush()
>          l=str(os.fstat(f.fileno()).st_size)
>     response('200 OK', [('Content-Type', 'text/plain'), ('Content-
> Length', str(len(l)))])
>     return [l]

Update it works on mod_wsgi but not on wsgiref.simple_server

C:\Users\gert\Desktop\hg\appwsgi\wsgi>curl -T upload2.wsgi
http://localhost/appwsgi/wsgi/upload2.wsg
i -v
* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> PUT /appwsgi/wsgi/upload2.wsgi HTTP/1.1
> User-Agent: curl/7.19.4 (amd64-pc-win32) libcurl/7.19.4 OpenSSL/0.9.8j zlib/1.2.3
> Host: localhost
> Accept: */*
> Content-Length: 869
> Expect: 100-continue
>
* Done waiting for 100-continue

I not getting 100-continues ?



More information about the Python-list mailing list