2

I have a client app which is using a PUT request to 'PUT' a zip archive at a source URL. I am trying to validate the content from the PUT request. My test framework is in python, so I am trying to start a basic python server to listen for PUT requests and write the file 'PUT' to disk.

I wanted to use the simpleHTTPServer, but it does not support PUT.

python -m SimpleHTTPServer 9009 Serving HTTP on 0.0.0.0 port 9009 ... [23/Jan/2018 06:49:52] code 501, message Unsupported method ('PUT') [23/Jan/2018 06:49:52] "PUT /a3866ad0-004c-11e8-99b0-ab49af452a48.zip HTTP/1.1" 501

I figured I could write my own custom requestHandler class for the PUT and just write the content to disk based on its length, but the PUT no content-length header, so I can't do this either.

class SputHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_PUT(self):
    print "headers"
    print self.headers
    print "---- done headers ----"
    length = int(self.headers["Content-Length"])
    with open("myTestZip.zip", "wb") as f:
        f.write(self.rfile.read(length))

However it seems the file is 'PUT' in multiple chunks, as there is no 'content-length' header and i see a 'transfer-encoding' header as 'chunked'. I can see in the printed headers, the content-length doesn't exist.

Serving HTTP on 0.0.0.0 port 8000 ... headers host: myHostNameBlah:8000 content-type: application/zip Connection: close Transfer-Encoding: chunked

---- done headers ---- Exception happened during processing of request from ('clientIpAddress', 47362) Traceback (most recent call last): File "C:\Python27\lib\SocketServer.py", line 290, in _handle_request_noblock self.process_request(request, client_address) File "C:\Python27\lib\SocketServer.py", line 318, in process_request self.finish_request(request, client_address) File "C:\Python27\lib\SocketServer.py", line 331, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\Python27\lib\SocketServer.py", line 652, in init self.handle() File "C:\Python27\lib\BaseHTTPServer.py", line 340, in handle self.handle_one_request() File "C:\Python27\lib\BaseHTTPServer.py", line 328, in handle_one_request method() File "tserver.py", line 10, in do_PUT length = int(self.headers["Content-Length"]) File "C:\Python27\lib\rfc822.py", line 393, in getitem return self.dict[name.lower()] KeyError: 'content-length'

Is there a simpleHTTPServer PUT handler implementation that will allow me to write the PUT zip archive to disk to act on later. The solutions i have found are all using the content-length header, is there a way to write the contents to disk without this header?

Or is this an issue with the client? Is it required it sends a content-length header? My understanding is if the data is chunked, then the content-length header is not needed.

Thank you, Alex

Alex
  • 21
  • 2
  • This might not answer your question however: The defined function doesn't seem to be in the class There is "indention" missing in the class definition Maybe have a look here [Click here](https://stackoverflow.com/questions/38405772/python-2-7-indentationerror#38405834rllib.request.Request) – user839099 May 25 '19 at 14:19

0 Answers0