Comment

Comment by mg

I like the first code example on https://crystal-lang.org

    # A very basic HTTP server
    require "http/server"

    server = HTTP::Server.new do |context|
        context.response.content_type = "text/plain"
        context.response.print "Hello world, got #{context.request.path}!"
    end

    puts "Listening on http://127.0.0.1:8080"
    server.listen(8080)
Nicely concise!

This would be the Python equivalent:

    # A very basic HTTP server
    from http.server import BaseHTTPRequestHandler, HTTPServer

    class RequestHandler(BaseHTTPRequestHandler):
       def do_GET(self):
           self.send_response(200)
           self.send_header('Content-type', 'text/plain')
           self.end_headers()
           self.wfile.write(bytes(f"Hello world, got {self.path}!", 'utf8'))

    httpd = HTTPServer(('', 8080), RequestHandler)
    print("Listening on http://127.0.0.1:8080")
    httpd.serve_forever()
Would like to see it in more languages.
parent mg 2 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton