当我在“lisp的土地”第12章和第13章学习关于“插座”的时候。 我使用lispbox,所以我需要安装一个socket。最后,我发现 usocket 可用。 这是一个关于如何使用 usocket 来构建网络服务器的示例。
(defun serve (request-handler)
"serve as a web server,used to handle request
like 'http://127.0.0.1:8080/greeting'"
(let ((socket (usocket:socket-listen "0.0.0.0" 8080 :reuse-address t)))
(unwind-protect
(loop (with-open-stream (stream
(usocket:socket-stream
(usocket:socket-accept socket)))
(let* ((url (parse-url (read-line stream)))
(path (car url))
(header (get-header stream))
(params (append (cdr url)
(get-content-params stream header)))
(*standard-output* stream))
(funcall request-handler path header params))))
(usocket:socket-close socket))))
http://landoflisp.com/webserver.lisp
中的原点'服务'功能(defun serve (request-handler)
(let ((socket (socket-server 8080)))
(unwind-protect
(loop (with-open-stream (stream (socket-accept socket))
(let* ((url (parse-url (read-line stream)))
(path (car url))
(header (get-header stream))
(params (append (cdr url)
(get-content-params stream header)))
(*standard-output* stream))
(funcall request-handler path header params))))
(socket-server-close socket))))
答案 0 :(得分:0)