Skip to content

Instantly share code, notes, and snippets.

@rizsotto
Created July 4, 2014 11:01
Show Gist options
  • Save rizsotto/73c0475ed694befc3058 to your computer and use it in GitHub Desktop.
Save rizsotto/73c0475ed694befc3058 to your computer and use it in GitHub Desktop.
HTTP POST with standard python modules
import http.server
import socketserver
import logging
import urllib.parse as p
PORT = 8000
class ServerHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
logging.error(self.headers)
http.server.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
# Extract and print the contents of the POST
length = int(self.headers['Content-Length'])
post_data = p.parse_qs(self.rfile.read(length).decode('utf-8'))
logging.error(post_data)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
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