How does the file.seek() work ?

G

gert

I want the file pointer set to 100 and overwrite everything from there

curl -C 100 -T upload2.wsgi http://192.168.2.17/appwsgi/wsgi/upload2.wsgi
-v

w+ overwrites my file completely
r+ overwrites nothing
a+ only makes my file bigger

import os

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]

also why must I open the file a second time to know how big it is ?
 
T

Tim Chase

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 you want to lop off the rest of the file, use f.truncate()

An example:

# create the initial file
# demonstrate that it worked
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
 
G

gert

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 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

Doh! my curl was wrong, thanks :)

curl -C 100 upload2.wsgi http://192.168.2.17/appwsgi/wsgi/upload2.wsgi
-v
 
G

Graham Dumpleton

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
 
G

gert

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()
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.wsgi http://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]
 
G

gert

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.

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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top