Last active
December 29, 2015 00:19
-
-
Save yogendra/7585031 to your computer and use it in GitHub Desktop.
Simple HTTP Post (Raw) Handling Server #Python #HTTP
This file contains hidden or 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
import SimpleHTTPServer | |
import SocketServer | |
import cgi | |
import logging | |
import base64 | |
import time | |
import os | |
ts = time.time() | |
uploadDir="upload/" | |
if not os.path.exists(uploadDir): | |
os.mkdir(uploadDir) | |
os.mkdir(uploadDir + str(ts)) | |
uploadPrefix = uploadDir+ str(ts) + "/" | |
uploadSuffix = "" | |
filecounter=0 | |
PORT = 8000 | |
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
logging.error(self.headers) | |
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) | |
def do_POST(self): | |
self.save_file() | |
def do_PUT(self): | |
self.save_file() | |
def save_file(self): | |
global filecounter | |
global uploadPrefix | |
global uploadSuffix | |
logging.error(self.headers) | |
filecounter += 1 | |
content_length = int(self.headers['Content-Length']) | |
path = self.path | |
if not path or path == "/" : | |
path= uploadPrefix + str(filecounter)+ uploadSuffix | |
else: | |
filedir = os.path.dirname(path) | |
if not os.path.exists(filedir) : | |
os.makedirs(filedir) | |
path=uploadPrefix+path | |
logging.error("Saving file to : " + path) | |
post_data = self.rfile.read(content_length) | |
with open(path, 'w+') as file: | |
file.write(post_data) | |
Handler = ServerHandler | |
httpd = SocketServer.TCPServer(("", PORT), Handler) | |
print "serving at port", PORT | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment