4 ms·
I just asked to create an HTTP server in python that returned the code I am a tea pot, this is what it said. Not exactly what I wanted it to do lol (Using a pa
by ajdude 4y ago
I just asked to create an HTTP server in python that returned the code I am a tea pot, this is what it said. Not exactly what I wanted it to do lol
(Using a paste bin link because of HN formatting)
https://pastebin.com/hcBubZUn https://pastebin.com/hcBubZUn
- mdaniel 4y agoBecause I think this conversation is better with the context here and not pastebin, I'm going to copy-paste your paste: ---BEGIN QUOTE--- Me: Provide the code for an http://server http://server in python that returns the code I am a teapot Vicuna: Here is an example of a simple Python HTTP server that returns the text "I am a teapot" when a GET request is made to the root endpoint: import http.server import socketserver class TeapotHTTPServer(http.server.SimpleHTTPRequestHandler): def end_headers(self): self.send_response(200) self.send_header('Content-type', 'text/plain') self.end_headers() self.wfile.write(b'I am a teapot') Handler = TeapotHTTPServer with socketserver.TCPServer(("localhost", 8000), Handler) as httpd: print("serving at port", httpd.address()) httpd.serve_forever() This code uses the http.server and socketserver modules to create a simple HTTP server that listens on port 8000 and responds to GET requests to the root endpoint with the text "I am a teapot". ---END QUOTE--- A++ for variable naming, A+++ for knowing `self.wfile.write` accepts `byte` not `str`, F for using the wrong method name, and C- for not knowing `send_response(418)` :-D
- mdaniel 4y agoYou 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 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?)