This repository was archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"mime/multipart" | ||
"net" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) { | ||
switch r.Method { | ||
case "POST": | ||
// parse the multipart form in the request with a 1MB max | ||
err := r.ParseMultipartForm(1 << 20) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// write each uploaded file to disk | ||
for _, fheaders := range r.MultipartForm.File { | ||
for _, hdr := range fheaders { | ||
// open uploaded | ||
var infile multipart.File | ||
infile, err = hdr.Open() | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
// open destination file | ||
var outfile *os.File | ||
outfile, err = os.Create("./" + hdr.Filename) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
// save the data to the file | ||
var written int64 | ||
written, err = io.Copy(outfile, infile) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
log.Printf("uploaded file: %s (%d bytes)", hdr.Filename, written) | ||
} | ||
} | ||
break | ||
default: | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
} | ||
}) | ||
|
||
http.Handle("/", http.FileServer(http.Dir("./"))) | ||
|
||
ls, err := net.Listen("tcp4", ":8000") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Println("Listening on 0.0.0.0:8000...") | ||
log.Fatal(http.Serve(ls, nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from http.server import SimpleHTTPRequestHandler, HTTPServer | ||
import logging, multipart | ||
|
||
class Handler(SimpleHTTPRequestHandler): | ||
|
||
protocol_version = 'HTTP/1.1' | ||
|
||
def do_POST(self): | ||
content_length = int(self.headers['Content-Length']) | ||
|
||
env = { | ||
"CONTENT_TYPE" : self.headers['Content-Type'], | ||
"REQUEST_METHOD": "POST", | ||
"CONTENT_LENGTH": content_length, | ||
"wsgi.input": self.rfile, | ||
} | ||
|
||
_, files = multipart.parse_form_data(env) | ||
for k, v in files.items(): | ||
v.save_as(k) | ||
|
||
body = "Uploaded {}bytes on path {}\n".format(self.path, content_length).encode('utf-8') | ||
self.send_response(200) | ||
self.send_header('Content-length', len(body)) | ||
self.end_headers() | ||
self.wfile.write(body) | ||
|
||
def run(server_class=HTTPServer, handler_class=Handler, port=8000): | ||
logging.basicConfig(level=logging.INFO) | ||
server_address = ('', port) | ||
httpd = server_class(server_address, handler_class) | ||
logging.info('Serving HTTP on 0.0.0.0 port %d (http://0.0.0.0:%d/) ...', port, port) | ||
try: | ||
httpd.serve_forever() | ||
except KeyboardInterrupt: | ||
pass | ||
httpd.server_close() | ||
logging.info('Interrupt received, exiting.') | ||
|
||
if __name__ == '__main__': | ||
from sys import argv | ||
|
||
if len(argv) == 2: | ||
run(port=int(argv[1])) | ||
else: | ||
run() |