Guest User

Untitled

a guest
Mar 30th, 2023
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. Me: Provide the code for an http://server in python that returns the code I am a teapot
  2.  
  3. 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:
  4. import http.server
  5. import socketserver
  6.  
  7. class TeapotHTTPServer(http.server.SimpleHTTPRequestHandler):
  8. def end_headers(self):
  9. self.send_response(200)
  10. self.send_header('Content-type', 'text/plain')
  11. self.end_headers()
  12. self.wfile.write(b'I am a teapot')
  13.  
  14. Handler = TeapotHTTPServer
  15.  
  16. with socketserver.TCPServer(("localhost", 8000), Handler) as httpd:
  17. print("serving at port", httpd.address())
  18. httpd.serve_forever()
  19. 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