In Ruby, you can create a simple HTTP server using the socket library. Here's an example of how to create a server that listens on port 8000 and responds to incoming requests with a 200 OK status code and a simple message:
main.rb258 chars16 lines
This code listens for incoming connections using TCPServer on localhost (you can change this to a specific IP address or hostname if you want to listen on a specific interface). It then accepts incoming connections from clients and reads the first line of the request using gets.
After that, it creates a response with the HTTP/1.1 200 OK status code, sets the Content-Type header to text/plain, and adds an extra \r\n to separate the headers from the content. The content itself is just the string "Hello, World!".
Finally, it sends the response to the client using puts and closes the connection.
Note that this is a very simple example and does not handle more advanced HTTP features like query parameters or response codes other than 200 OK.
gistlibby LogSnag