Racket Web服务器写入文件错误

时间:2016-06-29 14:27:05

标签: racket

我写了一个Web程序来写文件,然后读取文件。它失败了。

程序代码如下:

#lang web-server/insta

(define (start request)
  (index-wrap request))

(define (index-wrap request)
  (define (response-generator embed/url)
    (response/xexpr
      `(html
         (head (title "test"))
         (body
           (h1 "Test Write")
           (form ((action ,(embed/url write-handler)))
                  (input ((type "submit"))))))))

  (define (write-handler request)
    (call-with-output-file "write_file"
                              #:exists 'truncate
                              (lambda (out) (print "hello,world" out)))
  (index-wrap request))
  (send/suspend/dispatch response-generator))

当您单击该按钮时,会出现错误:

call-with-output-file: cannot open output file
path: /usr/share/racket/pkgs/web-server-lib/web-server/default-web-root/htdocs/write_file
system error: Permission denied; errno=13

我还编写了一个测试程序来测试call-with-output-file函数,并且写入文件成功。我的测试程序代码如下:

#lang racket

(call-with-output-file "write_other"
                          #:exists 'truncate
                          (lambda (out) (print "hello,racket" out)))

那么它的问题是什么?

1 个答案:

答案 0 :(得分:1)

尝试

(define (write-handler request)
    (displayln (list "HERE" (current-directory)))
    (call-with-output-file "write_file"
                              #:exists 'truncate
                              (lambda (out) (print "hello,world" out)))
  (index-wrap request))
  (send/suspend/dispatch response-generator))

查看您要写入的目录。我怀疑网络服务器已将目录更改为您预期的其他内容。

修改

现在你知道路径是

/usr/share/racket/pkgs/web-server-lib/web-server/default-web-root/htdocs/

你有两种选择。

  1. 更改文件夹的权限
  2. 在其他地方启动网络服务器
  3. 最简单的方法是更改​​权限:

     cd /usr/share/racket/pkgs/web-server-lib/web-server/default-web-root/
     chmod +w htdocs                 
     sudo chmod +w htdocs     
    

    希望最后两行之一有效。如果不是你需要 谷歌如何设置文件权限以在您的操作系统上写入。