Skip to content

Instantly share code, notes, and snippets.

@Malayke
Created June 9, 2022 07:55
Show Gist options
  • Save Malayke/dbdb94d498301068f68f3fbc666eb074 to your computer and use it in GitHub Desktop.
Save Malayke/dbdb94d498301068f68f3fbc666eb074 to your computer and use it in GitHub Desktop.
python http put server
#!/usr/bin/env python3
import sys
import signal
from threading import Thread
from http.server import HTTPServer, BaseHTTPRequestHandler
class PUTHandler(BaseHTTPRequestHandler):
def do_PUT(self):
length = int(self.headers['Content-Length'])
content = self.rfile.read(length)
file_name = self.path[1:]
with open(self.path[1:], "w") as f:
f.write(content.decode())
self.send_response(200)
self.wfile.write(f"file {file_name} recived".encode())
def run_on(port):
print("Starting a HTTP PUT Server on {0} port {1} (http://{0}:{1}) ...".format(sys.argv[1], port))
print("send file command: curl -T /etc/passwd localhost:1234")
server_address = (sys.argv[1], port)
httpd = HTTPServer(server_address, PUTHandler)
httpd.serve_forever()
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage:\n\tpython {0} ip 1337".format(sys.argv[0]))
sys.exit(1)
ports = [int(arg) for arg in sys.argv[2:]]
try:
for port_number in ports:
server = Thread(target=run_on, args=[port_number])
server.daemon = True # Do not make us wait for you to exit
server.start()
signal.pause() # Wait for interrupt signal, e.g. KeyboardInterrupt
except KeyboardInterrupt:
print("\nPython HTTP PUT Server Stoped.")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment