Skip to content

Instantly share code, notes, and snippets.

@tleepa
Forked from darkr4y/httpsrv.py
Last active October 13, 2022 12:07
Show Gist options
  • Save tleepa/58472e948115de7733d59ccf4adccf55 to your computer and use it in GitHub Desktop.
Save tleepa/58472e948115de7733d59ccf4adccf55 to your computer and use it in GitHub Desktop.
python simple http server with upload & download
#!/usr/bin/env python
"""Extend Python's built in HTTP server to save files
curl or wget can be used to send files with options similar to the following
curl -X PUT --upload-file somefile.txt http://localhost:8000
wget -O- --method=PUT --body-file=somefile.txt http://localhost:8000/somefile.txt
__Note__: curl automatically appends the filename onto the end of the URL so
the path can be omitted.
Windows upload & download
powershell -ep bypass -c "$wc=New-Object Net.WebClient;$wc.UploadFile('http://target.com/upload.bin', 'PUT', 'c:\\upload.bin');"
powershell -ep bypass -c "$wc=New-Object Net.WebClient;$wc.DownloadFile('http://target.com/download.bin','c:\\download.bin');"
Linux upload & download
curl -X PUT --upload-file upload.bin http://target.com/upload.bin
wget -O- --method=PUT --body-file=upload.bin http://target.com/upload.bin
wget http://target.com/download.bin -O /tmp/download.bin
curl http://target.com/download.bin -o /tmp/download.bin
"""
import os
import http.server as server
class HTTPRequestHandler(server.SimpleHTTPRequestHandler):
"""Extend SimpleHTTPRequestHandler to handle PUT requests"""
def do_PUT(self):
"""Save a file following a HTTP PUT request"""
filename = os.path.basename(self.path)
# Don't overwrite files
if os.path.exists(filename):
self.send_response(409, "Conflict")
self.end_headers()
reply_body = f"'{filename}' already exists\n"
self.wfile.write(reply_body.encode("utf-8"))
return
file_length = int(self.headers["Content-Length"])
read = 0
with open(filename, "wb+") as output_file:
while read < file_length:
new_read = self.rfile.read(min(66556, file_length - read))
read += len(new_read)
output_file.write(new_read)
self.send_response(201, "Created")
self.end_headers()
reply_body = f"Saved '{filename}'\n"
self.wfile.write(reply_body.encode("utf-8"))
if __name__ == "__main__":
server.test(HandlerClass=HTTPRequestHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment