Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Me: Provide the code for an 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".
Advertisement
Add Comment
Please, Sign In to add comment