#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    2
    Rep Power
    0

    fileupload in Python ?


    hi,

    i wrote a chat programm with SimpleXMLRPCServer. Now I've to write a code which uploads any kind of file to another client.
    I got the tipp opening the file in binary mode, which I tried also and didn't work the way I wanted. It gave something in unicode, ascii code whatever back.
    Actually it should open the file in " 0 1 " - format and than I've to send it to the server as an array.
    So my question is how to open the file in 01 - format?
    I searched the internet and found something with the cgi module , but I didn't understand how to use it.
    Hope someone could help me out.

    greets,

    cyberinder


    code which opens a file and saves it somewhere :

    Code:
      
    data=""
    def save(path):
        global data
        f=open(path,'w')
        f.truncate()
        f.close()
        f=open(path,'ab')
        f.write(str(data[0]))                     
        f.close()
    
    def open(path):
        global data
        f=open(path,"rb")
        data=f.readlines(),[]
      #  print data
        f.close()
        return data
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    There is an unpload function in my Acticle on devshed, not sure if this will help though since it is CGI based and your using XMLRPC and creating a web service - or i assume, but worth a look.

    http://www.devshed.com/c/a/Python/Python-on-the-Web/

    As for opening the file in '01' mode, is this not binary since binary code consists of 0 and 1 bit values? Anyway i believe if you open an ASCII file in binary mode it still gets treated as ACSII - it doesn't convert it to binary; if you need that to happen then you could probably fo it manually with the XOR operator but i havn't tried this yet.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    2
    Rep Power
    0
    hi,

    thanks for your help but somehow I didn't understand the part with the upload etc.

    So far I tried my upper code and it worked .

    Code:
    data=""
    def save(path):
        global data
        f=open(path,'wb')# instead of "w" , "wb"
        f.truncate()
        f.close()
        f=open(path,'ab')
        f.write(str(data[0]))                     
        f.close()
    
    def open(path):
        global data
        f=open(path,"rb")
        data=f.readlines(),[]
      #  print data
        f.close()
        return data
    With that code I can "copy" a file on my local computer. Now the problem with the xmlrpclib Protocol is that you can't send any exe, jpg, pdf etc files. It accepts only text files which do not have any unknown character like , , etc.
    Hope someone knows a way out of that problem.

    greetz,
    Cyberinder

IMN logo majestic logo threadwatch logo seochat tools logo