Skip to content
TheChatArchiveThe Chat Archive
DeepSeekdeepseek-r1-0528· DeepSeekHugging Face LMArena Human Preference 140K

make a lua script to create a http webserver

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with grok-3-mini-beta’s answer.
  1. User

    make a lua script to create a http webserver

  2. DeepSeek Assistant

    Here's a Lua script that creates a basic HTTP web server using LuaSocket:

    local socket = require("socket")
    
    -- Configuration
    local HOST = "0.0.0.0"  -- Listen on all interfaces
    local PORT = 8080
    local MAX_CONNECTIONS = 10
    local RESPONSE_HEADERS = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"
    
    -- Create server socket
    local server = assert(socket.bind(HOST, PORT))
    server:settimeout(0)  -- Non-blocking mode
    print("Server running at http://" .. HOST .. ":" .. PORT)
    
    -- Main loop
    while true do
        local client = server:accept()
        if client then
            client:settimeout(5)  -- 5-second timeout for client operations
            local request, err = client:receive("*l")  -- Read first line (request line)
            
            if not err then
                -- Parse request line (GET /path HTTP/1.1)
                local method, path = request:match("^(%w+)%s+(%S+)%s+HTTP/[%d%.]+$")
                method = method and method:upper() or "UNKNOWN"
                path = path or "/"
                
                print("Request:", method, path)
                
                -- Read remaining headers (until empty line)
                while true do
                    local line, _ = client:receive("*l")
                    if not line or line == "" then break end
                end
                
                -- Send response
                local html = string.format([[
    <!DOCTYPE html>
    <html>
    <head><title>Lua Web Server</title></head>
    <body>
        <h1>Hello from Lua!</h1>
        <p>Server time: %s</p>
        <p>Request path: %s</p>
    </body>
    </html>
    ]], os.date("%Y-%m-%d %H:%M:%S"), path)
                
                client:send(RESPONSE_HEADERS .. html)
            end
            
            client:close()
        end
        socket.sleep(0.01)  -- Small delay to reduce CPU usage
    end
    

    Features:

    1. Listens on all network interfaces at port 8080
    2. Responds to all requests with a simple HTML page
    3. Shows current server time and requested path
    4. Handles basic HTTP parsing
    5. Non-blocking operation with timeout protection

    Requirements:

    • Install LuaSocket: luarocks install luasocket
    • Lua 5.1 or newer

    To use:

    1. Save as webserver.lua
    2. Run with: lua webserver.lua
    3. Visit in browser: http://localhost:8080 or http://your-server-ip:8080

    Customization:

    • Change PORT variable to use different port
    • Modify HTML content in the html variable
    • Adjust MAX_CONNECTIONS for concurrent handling
    • Add route handling by checking the path variable

    Example output in browser:

    Hello from Lua!
    Server time: 2023-09-15 14:30:45
    Request path: /test
    

    Security Notes:

    1. This is a basic educational example
    2. Not suitable for production use
    3. No request validation or security features
    4. No persistent connections (Connection: close header)
    5. Limited error handling

    For a more robust solution, consider:

    • Adding proper error handling
    • Implementing request size limits
    • Adding route-based handling
    • Supporting different content types
    • Adding security headers

    You can extend this foundation by:

    1. Adding route handlers:
    local routes = {
        ["/"] = function() return "<h1>Home Page</h1>" end,
        ["/about"] = function() return "<h1>About Page</h1>" end
    }
    
    -- In response section
    local content = routes[path] and routes[path]() or "<h1>404 Not Found</h1>"
    client:send(RESPONSE_HEADERS .. content)
    
    1. Adding file serving capability
    2. Implementing POST request handling
    3. Adding logging functionality
    4. Implementing HTTP basic authentication

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.