gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
can your give code of create server only nothing else
- User
can your give code of create server only nothing else
- Assistant
Certainly! Below is a simple example of a basic HTTP server in Python using the built-in
http.servermodule. This code creates a server that listens on port 8000 and responds with "Hello, world!" to any HTTP request:pythonimport http.server import socketserver PORT = 8000 class SimpleHTTPRequestHandler(http.server.SimpleHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(b"Hello, world!") Handler = SimpleHTTPRequestHandler with socketserver.TCPServer(("", PORT), Handler) as httpd: print(f"Serving on port {PORT}") httpd.serve_forever()To run this server:
- Save the code to a file, for example,
server.py. - Run it in your terminal with
python server.py.
- Save the code to a file, for example,