Skip to content

Instantly share code, notes, and snippets.

@bjorndown
Created October 31, 2013 11:40
Show Gist options
  • Save bjorndown/7248293 to your computer and use it in GitHub Desktop.
Save bjorndown/7248293 to your computer and use it in GitHub Desktop.
http echo server with python
from http.server import BaseHTTPRequestHandler
class PostHandler(BaseHTTPRequestHandler):
def do_POST(self):
self.answer()
self.print_delimiter()
self.print_request_header()
self.print_request_body()
self.print_delimiter()
return
def print_delimiter(self):
print("-" * 30)
def print_request_header(self):
print(self.headers)
def print_request_body(self):
content_length = int(self.headers["content-length"])
req_str = ''
if (content_length):
to_read = content_length
while len(req_str) < content_length:
_req_str = self.rfile.read(to_read)
to_read -= len(_req_str)
req_str += _req_str.decode('iso-8859-1')
print(req_str)
def answer(self):
self.send_header("Content-Type", "text/xml;charset=ISO-8859-1")
self.send_header("Accept", "text/xml;charset=ISO-8859-1")
self.send_response(200)
self.end_headers()
if __name__ == '__main__':
from http.server import HTTPServer
port = 12999
server = HTTPServer(('localhost', port), PostHandler)
print('Listening on localhost:' + str(port) + ', use <Ctrl-C> to stop')
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment