1

Novice question re. BaseHTTPRequestHandler and receiving data...

I've been using BaseHTTPRequestHandler to receive JSON strings passed as data to my URI. I now need to receive both JSON strings and ascii files. How can I tell that I've received both JSON data and a separate flat file? How do I access the data in the file?

What if I've received multiple files?

BTW, I just ran a test by calling my URI from Postman & see the following headers:

headers: Host: localhost:6081

Content-Type: application/x-www-form-urlencoded

User-Agent: python-requests/2.2.1 CPython/3.4.0 Linux/3.13.0-35-generic

Accept: */*

Accept-Encoding: gzip, deflate, compress

Content-Length: 403

Thank you!

Ben

Ben
  • 4,798
  • 3
  • 21
  • 35
  • Judging by those headers, your client is sending the files. See [this question](http://stackoverflow.com/questions/8659808/how-does-http-file-upload-work) for a general overview on how file upload works. (In your case, since you're using JSON rather than www-form-urlencoded, the details will be slightly different, but it's still going to be a MIME multipart message.) So, you need to fix that, before you worry about the server side. – abarnert Oct 28 '14 at 00:05
  • Anyway, `BaseHTTPRequestHandler`, especially the 2.x version, makes this pretty clunky. IIRC, the "right" way to do it is to manually use the MIME-parsing stuff in the `email` package to handle the body, but there's a hacky way to do it by substituting `email.message.Message` for the `MessageClass` class attribute in your subclass and using your `headers` attribute as if it were the envelope. – abarnert Oct 28 '14 at 00:12
  • Actually, digging up some old code, you need to pass a wrapper around `email.message.Message` that has a constructor that acts like `mimetools.Message`. Basically, it needs to take the `fp` argument and use it to feed the data to its superclass. Hopefully this is enough to get you started? – abarnert Oct 28 '14 at 00:30
  • @abarnert thanks for your help. We're using python 3+ and, after some experimentation have found that the cgi library help with this. I'll post this as an answer to my question. – Ben Nov 02 '14 at 16:12

1 Answers1

1

The answer is in the CGI library. Refer to the following StackOverflow post: Simple Python WebServer. The second answer in that post was most useful for us.

Here is some test code that you might find useful to print out what's going on behind the scenes, especially if you're trying to receive multiple files in one post:

        print("command: " + self.command + "\npath: " + self.path + "\nrequest_version: " \
            + self.request_version + "\nheaders: " + str(self.headers))
        form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD': 'POST',
                     'CONTENT_TYPE': self.headers['Content-Type'],
                     })
        print("\nform:", str(form))
        print("\nform['file'].filename:", form['file'].filename)
        filename = form['file'].filename
        data = form['file'].file.read()
        open("/tmp/%s" % filename, "wb").write(data)
        print('\ndata:', data)
Community
  • 1
  • 1
Ben
  • 4,798
  • 3
  • 21
  • 35