Hacker News new | ask | show | jobs
by mdaniel 1178 days ago
You inspired me to find out if it was good at answering your question because it's something it may have seen before, so I thought I'd be more cheeky:

---BEGIN CHAT---

Q: provide the code for an http server in python that exposes a "hello world" method over SOAP with document/literal encoding

A: Here is an example of an HTTP server in Python that exposes a "hello world" method over SOAP with document/literal encoding:

    from http.server import BaseHTTPRequestHandler, HTTPServer
    from urllib.parse import quote, unquote
    from io import BytesIO
    import xml.sax.saxutils as saxutils

    class HelloWorldHandler(BaseHTTPRequestHandler):
        def do_POST(self):
            content_length = int(self.headers['Content-Length'])
            content = self.rfile.read(content_length)
            self.send_response(200)
            self.send_header('Content-type', 'text/xml; charset=UTF-8')
            self.send_header('Content-encoding', 'document')
            self.end_headers()

            # Parse the SOAP request
            request = BytesIO(content)
            soap_request = request.getvalue()
            request.close()

            # Call the "hello world" method
            response = '<?xml version="1.0" encoding="UTF-8"?>\n<response>\n  <result>Hello, World!</result>\n</response>\n'
            response = saxutils.escape(response)

            # Send the SOAP response
            self.wfile.write(saxutils.escape(response))
            return

    def run(server_class=HTTPServer, handler_class=HelloWorldHandler, port=8080):
        server_address = ('', port)
        httpd = server_class(server_address, handler_class)
        print(f'Starting httpd on port {port}...')
        httpd.serve_forever()

    if __name__ == '__main__':
        run()
To use this code, simply run the script in the terminal and open a web browser to the following URL: http://localhost:8080/soap/helloWorld

---END CHAT---

A+ for variable naming again, nice job knowing about the `do_POST`, F for `content-encoding: document`, D for sending the byte array through BytesIO for no damn good reason, and F--- for saxutils.escaping the outgoing XML (we'll set aside the bogus invocation instructions because I didn't ask it for that, it just ... volunteered?)