Skip to content

Instantly share code, notes, and snippets.

@pavgup
Created June 16, 2023 20:27
Show Gist options
  • Save pavgup/ae1008027b24d27eecf240e3533fc31d to your computer and use it in GitHub Desktop.
Save pavgup/ae1008027b24d27eecf240e3533fc31d to your computer and use it in GitHub Desktop.
using curl to send a file to a remote machine
# on the remote server you want to use to send a file, you run:
# curl -F "file=@/log.txt" http://100.100.100.100:8000/
# this will create the file log.txt in /tmp that will ideally containt he log.txt from the remote machine
from http.server import SimpleHTTPRequestHandler
import os
class FileUploadHandler(SimpleHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
uploaded_file = self.rfile.read(content_length)
file_name = os.path.basename("log.txt")
# Provide the base directory path where the uploaded file should be stored
base_directory = '/tmp'
file_path = os.path.join(base_directory, file_name)
with open(file_path, 'wb') as file:
file.write(uploaded_file)
self.send_response(200)
self.end_headers()
self.wfile.write(b'File uploaded successfully')
if __name__ == '__main__':
from http.server import HTTPServer
server_address = ('', 8000)
httpd = HTTPServer(server_address, FileUploadHandler)
print('Serving HTTP on 0.0.0.0 port 8000...')
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment