Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one upload files using Python SimpleHTTPServer or cgi?

I would like to have a simple web page on which user can upload files. What would be the simplest way to do it.

I know how to start SimpleHTTPServer but I do not know how I can upload files using SimpleHTTPServer. I do not even know if it is possible.

I found some code for uploading files using cgi but if I execute this code in the command line it just prints me HTML code on the screen.

like image 567
Roman Avatar asked Dec 06 '13 16:12

Roman


People also ask

How do I transfer files using SimpleHTTPServer?

Go to the directory with the file you want to share using cd on *nix or MacOS systems or CD for Windows. Start your HTTP server with either python -m SimpleHTTPServer or python3 -m http. server. Open new terminal and type ifconfig on *nix or MacOS or ipconfig on Windows to find your IP address.

How do you upload a file in python?

Method 1: Using the Python's os Module: Also, the enctype attribute with "multi-part/form-data" value will help the HTML form to upload a file. Lastly, we need the input tag with the filename attribute to upload the file we want. Lastly, we need the input tag with the filename attribute to upload the file we want.

What does python SimpleHTTPServer do?

The SimpleHTTPServer module is a Python module that enables a developer to lay the foundation for developing a web server. However, as sysadmins, we can use the module to serve files from a directory. The module loads and serves any files within the directory on port 8000 by default.


2 Answers

Yes, SimpleHTTPServer can receive http uploads with the correct request handler.

Basicly you need to define a do_POST method where a form or something similar uploads the data. the upload is then readble from self.rfile.

class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
     # ...
     def do_POST(self):
         for line in self.rfile:
             # do something with the line
             print line

of course if you want to upload from a browser you need to have a form somewhere that posts to the http server:

<form enctype="multipart/form-data" method="post" action="http://hostname.of.server:8080/">
  <input name="file" type="file"/>
  <input type="submit" value="upload"/>
</form>

More details in particular how to parse the raw data from the form-data can be found in the links posted as comments to the question.

like image 159
textshell Avatar answered Sep 22 '22 03:09

textshell


I am still new to Python and have tried using the same code you added to your post. The only problem with it is that it only allows for single file upload. I wanted to upload multiple files at a time.

Using the still available code found here, you can replace the deal_post_data method with the following:

    form = cgi.FieldStorage(
    fp=self.rfile,
    headers=self.headers,
    environ={'REQUEST_METHOD':'POST'})

    self.send_response(200)
    self.end_headers()
    
    saved_fns = ""
    
    try:
        if isinstance(form['file'], list):
            for f in form['file']:
                print f.filename
                saved_fns = saved_fns + ", " + f.filename
                self.save_file(f)
                self.wfile.write(f.value)
        else:
            f = form['file']
            self.save_file(f)
            saved_fns = saved_fns + f.filename
            self.wfile.write(f.value)
        return (True, "File(s) '%s' upload success!" % saved_fns)
    except IOError:
        return (False, "Can't create file to write, do you have permission to write?")

Then add the following function to save the uploaded file:

def save_file(self, file):
    outpath = os.path.join("", file.filename)
    with open(outpath, 'wb') as fout:
        shutil.copyfileobj(file.file, fout, 100000)

Finally, change the html form to allow for multiple files to be uploaded at a time using the multiple tag in the inserted HTML.

I just finished testing this and it works fine.

Hope it is helpful

like image 23
C MaCleif Avatar answered Sep 18 '22 03:09

C MaCleif