How do I receive files via Python's BaseHTTPRequestHandler class?

Ben

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

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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I marshal a pointer to a cstring between Python and C via SWIG

来自分类Dev

How do I fix the output of my fraction class's overloaded + operator?

来自分类Dev

Python:BaseHTTPRequestHandler-阅读原始帖子

来自分类Dev

How do I parse a RestSharp response into a class?

来自分类Dev

How do I replace the behaviour of Web API model binding so that instead of Null I receive a new instance when no parameters are passed in

来自分类Dev

How do I rename multiple files by removing everything but numbers?

来自分类Dev

如何通过Python的BaseHTTPRequestHandler类接收文件?

来自分类Dev

如何通过Python的BaseHTTPRequestHandler类接收文件?

来自分类Dev

How do I use an extension function in another class? C#

来自分类Dev

How do I catch all of these exceptions in python?

来自分类Dev

I have a lot of netcdf files, how would I use xarray to upload all the files into one python notebook?

来自分类Dev

How do I give a list dynamically without hardcoding to MongoDB's $in?

来自分类Dev

How do I use matplotlib's event handing inside wxPython?

来自分类Dev

How do I apply decorators to a superclass's methods?

来自分类Dev

How do I programmatically disable Hazelcast client's logging?

来自分类Dev

How do I get the computer's current language in Go?

来自分类Dev

How can I do pagination when downloading facebook posts via javascript and FB.api?

来自分类Dev

How do I dynamically inject an Angular2 sub component via typescript code?

来自分类Dev

How do I create a Python CLR procedure in SQL Server?

来自分类Dev

How do i change the location directory of python open document?

来自分类Dev

how do I convert a double array in C to python list?

来自分类Dev

How do I setup gruntjs compass watch with multiple subdirectories of scss files

来自分类Dev

How do I real-time synchronize two files in Windows 8.1?

来自分类Dev

How do I make vim automatically apply c++ syntax highlight on Arduino files (.ino/.pde)?

来自分类Dev

python-将BaseHTTPRequestHandler与UnixStreamServer一起使用会导致异常

来自分类Dev

When do I generate new GUID's for COM Servers? (Examples in Python)

来自分类Dev

How do I iterate through each line of a command's output in bash?

来自分类Dev

How do I debug AWS Api Gateway & Lambda's "AWS/ApiGateway 5XXError"

来自分类Dev

Rails: How do I require ActiveSupport's rescue_from method?

Related 相关文章

  1. 1

    How do I marshal a pointer to a cstring between Python and C via SWIG

  2. 2

    How do I fix the output of my fraction class's overloaded + operator?

  3. 3

    Python:BaseHTTPRequestHandler-阅读原始帖子

  4. 4

    How do I parse a RestSharp response into a class?

  5. 5

    How do I replace the behaviour of Web API model binding so that instead of Null I receive a new instance when no parameters are passed in

  6. 6

    How do I rename multiple files by removing everything but numbers?

  7. 7

    如何通过Python的BaseHTTPRequestHandler类接收文件?

  8. 8

    如何通过Python的BaseHTTPRequestHandler类接收文件?

  9. 9

    How do I use an extension function in another class? C#

  10. 10

    How do I catch all of these exceptions in python?

  11. 11

    I have a lot of netcdf files, how would I use xarray to upload all the files into one python notebook?

  12. 12

    How do I give a list dynamically without hardcoding to MongoDB's $in?

  13. 13

    How do I use matplotlib's event handing inside wxPython?

  14. 14

    How do I apply decorators to a superclass's methods?

  15. 15

    How do I programmatically disable Hazelcast client's logging?

  16. 16

    How do I get the computer's current language in Go?

  17. 17

    How can I do pagination when downloading facebook posts via javascript and FB.api?

  18. 18

    How do I dynamically inject an Angular2 sub component via typescript code?

  19. 19

    How do I create a Python CLR procedure in SQL Server?

  20. 20

    How do i change the location directory of python open document?

  21. 21

    how do I convert a double array in C to python list?

  22. 22

    How do I setup gruntjs compass watch with multiple subdirectories of scss files

  23. 23

    How do I real-time synchronize two files in Windows 8.1?

  24. 24

    How do I make vim automatically apply c++ syntax highlight on Arduino files (.ino/.pde)?

  25. 25

    python-将BaseHTTPRequestHandler与UnixStreamServer一起使用会导致异常

  26. 26

    When do I generate new GUID's for COM Servers? (Examples in Python)

  27. 27

    How do I iterate through each line of a command's output in bash?

  28. 28

    How do I debug AWS Api Gateway & Lambda's "AWS/ApiGateway 5XXError"

  29. 29

    Rails: How do I require ActiveSupport's rescue_from method?

热门标签

归档