[BangPypers] file save from python script

Praveen Kumar praveen at mahiti.org
Fri Nov 7 13:13:36 CET 2008


Hi Attaullah thank you so much.
I tried that one too but that file is storing in data.fs which i do not
want. i want to store my file to be physically stored. so for that i am
trying to write a python script. i have written many script but could not
get succeed
#!/usr/local/bin/python
"""This demonstrates a minimal http upload cgi.
This allows a user to upload up to three files at once.
It is trivial to change the number of files uploaded.

This script has security risks. A user could attempt to fill
a disk partition with endless uploads.
If you have a system open to the public you would obviously want
to limit the size and number of files written to the disk.
*"""
import cgi
import cgitb; cgitb.enable()
import os, sys

UPLOAD_DIR = "/home/praveen/Desktop/UPLOAD"

def save_uploaded_file (form_field, upload_dir):
    """This saves a file uploaded by an HTML form.
       The form_field is the name of the file input field from the form.
       For example, the following form_field would be "file_1":
           <input name="file_1" type="file">
       The upload_dir is the directory where the file will be written.
       If no file was uploaded or if the field does not exist then
       this does nothing.
    """
    form = cgi.FieldStorage()  ///when i tried to print form field its
returning None
    if not form.has_key(form_field): return
    fileitem = form[form_field]
    if not fileitem.file: return
    fout = file (os.path.join(upload_dir, fileitem.filename), 'wb')
    while 1:
        chunk = fileitem.file.read(100000)
        if not chunk: break
        fout.write (chunk)
    fout.close()

save_uploaded_file ("/home/praveen/Desktop/1.html", UPLOAD_DIR)*

this script is not giving me error..

Is any one know script to save the file physically on the particluar
system.. suppose i have file in my Desktop and i have one interface(html
page with file text field) which provide to select you file and move to
another location /home/praveen/Desktop

Thanks

On Fri, Nov 7, 2008 at 4:49 PM, Ataulla S H <ataulla at gmail.com> wrote:

> hi praveen try this
>
>   myrepo = getattr(context,*'*mydir*'*)
>   myrepo.manage_addFile(id=id, title=title, file=file)
>
>
> On Fri, Nov 7, 2008 at 12:31 PM, Praveen Kumar <praveen at mahiti.org> wrote:
>
>> Hi First of all i would like to thank you for giving me some idea..
>> As Jorgen told i did in same way
>> Hi Jorgen
>> I created a python script in zope instance to upload multiple files and
>> its working successfully..
>> but its storing the file inside custom folder as we know context always
>> refer to current location i mean our-instance/custom but when i am giving
>> container['bin'] its giving me 'bin' key error. i want to save that file
>> inside my instance for example
>>
>> myzope-instance/mydir/*<uploaded file>* is any one have any idea
>>
>> *REQUEST=context.REQUEST
>> sentFiles = {}
>> for key in context.REQUEST.keys():             # getting all the ids
>>     if key[:5]=='file.' and key[-3:]=='.id':   # we finding id
>> file.XXX.id
>>             sentFiles[REQUEST[key]]=''
>>
>> for key in context.REQUEST.keys():
>>     if key[:5]=='file.' and key[-3:]!='.id':    #we've got a file
>>         #found its id and make sure file exists
>>         if sentFiles.has_key(REQUEST[key+'.id']) and
>> REQUEST[key].filename:
>>             sentFiles[REQUEST[key+'.id']]=REQUEST[key]
>>         else:
>>             del sentFiles[REQUEST[key+'.id']]    #remove keys that dont
>> have files
>>
>> for k in sentFiles.keys():
>>     **context.manage_addFile(k,sentFiles[k])*
>> *    container['bin'].mydir.manage_addFile(k,sentFiles[k])
>>
>> return str(len(sentFiles)) + ' files has been uploaded successfully'*
>>
>>
>>
>> On Thu, Nov 6, 2008 at 7:49 PM, Jørgen Jørgensen <gardsted at yahoo.com>wrote:
>>
>>>  Hello Praween Kumar -
>>> I would make a pythonscript on the zope like
>>> 'locahost:8080/starinstance/myscript', taking 3 parameters, id, title and
>>> file.
>>> remember to give it manager proxy rights (proxy in top of python script
>>> edit screen, chose manager)
>>> script should be something like:
>>> """
>>> container['bin'].manage_addFile(id=id, title=title, file=file)
>>> """
>>> I havent tested, sorry;-), but at least the direction should be something
>>> like this.
>>> then change addfileurl to:
>>>
>>> *addfileurl='http://localhost:8080/starinstance/myscript<http://localhost:8080/starinstance/bin/manage_addFile>
>>> '*
>>>
>>> kind regards jorgen / denmark
>>>
>>>
>>> Praveen Kumar wrote:
>>>
>>> Dear all,
>>> I want to save the file on filebased system. i am using urllib
>>>
>>> here i tried with this code
>>>
>>> *import urllib
>>>
>>> # fill these in
>>> user='admin'
>>> password='admin1'
>>> addfileurl='http://localhost:8080/starinstance/bin/manage_addFile'
>>> useragent="upload.py/0.1"
>>> filename='/home/praveen/Desktop/text.jpeg'
>>> filetitle='a nice test picture'
>>>
>>> class MyUrlOpener(urllib.FancyURLopener):
>>>    def prompt_user_passwd(self, host, realm):
>>>         return (user,password)
>>>    def __init__(self, *args):
>>>       self.version = useragent
>>>       urllib.FancyURLopener.__init__(self, *args)
>>>
>>> def main():
>>>        # use authentication and set the user agent
>>>        urllib._urlopener = MyUrlOpener()
>>>
>>>        # read the contents of filename into filebody
>>>        f=open(filename)
>>>        filebody=f.read()
>>>        f.close
>>>
>>>        # urlencode the id, title and file
>>>        params = urllib.urlencode({'id': filename,
>>>        'title':filetitle,
>>>        'file':filebody})
>>>
>>>        # send the file to zope
>>>        f=urllib.urlopen(addfileurl, params)
>>>
>>> if __name__ == '__main__':
>>>    main()*
>>>
>>> when i compile this file with simple python demo.py it gives me error
>>> Traceback (most recent call last):
>>>   File "demo.py", line 36, in ?
>>>     main()
>>>   File "demo.py", line 33, in main
>>>     f=urllib.urlopen(addfileurl, params)
>>>   File "/usr/lib/python2.4/urllib.py", line 84, in urlopen
>>>     return opener.open(url, data)
>>>   File "/usr/lib/python2.4/urllib.py", line 192, in open
>>>     return getattr(self, name)(url, data)
>>>   File "/usr/lib/python2.4/urllib.py", line 272, in open_http
>>>     import httplib
>>>   File "/usr/lib/python2.4/httplib.py", line 70, in ?
>>>     import mimetools
>>>   File "/usr/lib/python2.4/mimetools.py", line 6, in ?
>>>     import tempfile
>>>   File "/usr/lib/python2.4/tempfile.py", line 33, in ?
>>>     from random import Random as _Random
>>>   File "/usr/lib/python2.4/random.py", line 43, in ?
>>>     from math import log as _log, exp as _exp, pi as _pi, e as _e
>>> ImportError: /usr/lib/python2.4/lib-dynload/math.so: undefined symbol:
>>> PyFPE_jbuf
>>>
>>> so i compiled with this command *python2.4 demo.py* it does not give any
>>> error
>>> but it does not give me output to when i check my directory where i am
>>> trying to save my jpeg file *
>>> http://localhost:8080/starinstance/bin/manage_addFile *there i do not
>>> find that file it means it is not able to store that file.
>>>
>>> i do not know what i am trying to do..could any one please give me idea
>>> how may i store the file on file system.
>>> i created a html page
>>>      <form name="demo" method="post" ENCTYPE="multipart/form-data"
>>> ACTION="">
>>>     <fieldset style="width:20%">
>>>
>>>         Id:<input type="text" name="title" id="title"/><br><br>
>>>         Name:<input type="text" name="name" id="name"/><br><br>
>>>         Keywords:<input type="text" name="keywords"
>>> id="keywords"/><br><br>
>>>         <input type="file" name="upload" id="upload"/><br><br>
>>>         <input type="submit" name="submit" value="Save" id="submit"/><br>
>>>     </fieldset>
>>>      </form>
>>> once if user select any file it should store to specific location *
>>> http://localhost:8080/starinstance/bin/manage_addFile
>>>
>>> *Any suggestion will be appreciable
>>>
>>> Thanks.
>>> --
>>> Praveen Kumar
>>> Software Engineer
>>> Mahiti Infotech Pvt. Ltd.
>>> # 33-34, Hennur Cross
>>> Hennur Main Road
>>> Bangalore, India - 560043
>>> Mobile:  +91 9343297314
>>>             +91 9739854134
>>> http://www.mahiti.org
>>>
>>> ------------------------------
>>>
>>> _______________________________________________
>>> BangPypers mailing listBangPypers at python.orghttp://mail.python.org/mailman/listinfo/bangpypers
>>>
>>>
>>> _______________________________________________
>>> BangPypers mailing list
>>> BangPypers at python.org
>>> http://mail.python.org/mailman/listinfo/bangpypers
>>>
>>>
>>
>>
>> --
>> Praveen Kumar
>> Software Engineer
>> Mahiti Infotech Pvt. Ltd.
>> # 33-34, Hennur Cross
>> Hennur Main Road
>> Bangalore, India - 560043
>> Mobile:  +91 9343297314
>>             +91 9739854134
>> http://www.mahiti.org
>>
>> _______________________________________________
>> BangPypers mailing list
>> BangPypers at python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>>
>
>
> --
> Ataulla SH
>
> web:www.kring.com
> personal blog:www.ataulla.objectis.net
> KRING Technologies India Pvt. Ltd.
> 1st Floor, Tower B, Infinity Towers, DLF II, Gurgaon-122 002
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>


-- 
Praveen Kumar
Software Engineer
Mahiti Infotech Pvt. Ltd.
# 33-34, Hennur Cross
Hennur Main Road
Bangalore, India - 560043
Mobile:  +91 9343297314
            +91 9739854134
http://www.mahiti.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20081107/6eabd2f1/attachment-0001.htm>


More information about the BangPypers mailing list