Skip to content

Instantly share code, notes, and snippets.

@jewel-andraia
Created August 7, 2018 21:47
Show Gist options
  • Save jewel-andraia/e4725876a498d51d3ad85e9a3a3808c0 to your computer and use it in GitHub Desktop.
Save jewel-andraia/e4725876a498d51d3ad85e9a3a3808c0 to your computer and use it in GitHub Desktop.
Echo POST request
import SimpleHTTPServer
import SocketServer
import json
from datetime import datetime
PORT = 8443
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_POST(self):
request_path = self.path
request_headers = self.headers
content_length = request_headers.getheaders('content-length')
length = int(content_length[0]) if content_length else 0
request_body = self.rfile.read(length)
try:
body = json.loads(request_body)
except ValueError:
body = '"{}"'.format(request_body)
print("\n----- Request Start ----->\n")
print(request_path)
print(str(datetime.now()))
print(request_headers)
print json.dumps(body, indent=2, sort_keys=True)
print("<----- Request End -----\n")
self.send_response(200)
self.end_headers()
if __name__ == "__main__":
httpd = SocketServer.TCPServer(("", PORT), ServerHandler)
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