Chinaunix首页 | 论坛 | 博客
  • 博客访问: 959186
  • 博文数量: 104
  • 博客积分: 3715
  • 博客等级: 中校
  • 技术积分: 1868
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-30 08:38
文章分类

全部博文(104)

文章存档

2013年(1)

2012年(9)

2011年(41)

2010年(3)

2009年(3)

2008年(47)

分类: Python/Ruby

2012-03-20 20:46:25


点击(此处)折叠或打开

  1. #!/usr/bin/env guile
  2. !#

  3. ;; A command line http client written in guile scheme.
  4. (use-modules (ice-9 format)
  5.              (ice-9 rdelim)
  6.              (ice-9 getopt-long)
  7.              (web uri)
  8.              (web request)
  9.              (web response)
  10.              (web client)
  11.              (rnrs bytevectors))

  12. ;; command line options' specification
  13. (define option-spec
  14.     '(version (single-char #\v))
  15.        (help (single-char #\h))
  16.        (method (single-char #\m) (value #t))
  17.        (body (single-char #\b) (value #t))))

  18. (define options (getopt-long (command-line) option-spec))
  19. (define args (option-ref options '() '()))
  20. (define program (car (command-line)))
  21. (define version "1.0")

  22. ;; print usage and exit with errno
  23. (define (usage errno)
  24.     (let ((errport (current-error-port)))
  25.           (format errport "Usage: ~a [options] request-url~%" program)
  26.           (for-each (lambda (opt desc) (format errport "\t~a: ~a~%" opt desc))
  27.                 '("--version,-v"
  28.                   "--help,-h"
  29.                   "--method,-m"
  30.                   "--body,-b")
  31.                 '("print version information"
  32.                   "print this help message"
  33.                   "specify http request method, eg: GET POST"
  34.                   "specify http request body")))
  35.           (exit errno))

  36. ;; print version and exit normaly
  37. (define (print-version)
  38.     (format #t "Version: ~a\n" version)
  39.     (exit 0))

  40. ;; perform a http request
  41. (define (http-request req-uri)
  42.     (define req (build-request req-uri
  43.                      #:method (string->symbol
  44.                                   (option-ref options 'method "GET"))))
  45.     (define req-body (option-ref options 'body #f))
  46.     (define sport (open-socket-for-uri req-uri))
  47.     (let ((opened-request (write-request req sport)))
  48.          (if req-body
  49.              (write-request-body opened-request (string->utf8 req-body))))
  50.     (force-output sport)
  51.     (shutdown sport 1)

  52.     ;; get result and print out
  53.     (let loop ((line (read-line sport)))
  54.          (if (eof-object? line)
  55.              (write-line "")
  56.              (begin
  57.                  (write-line line)
  58.                  (loop (read-line sport))))))

  59. ;; main entry point
  60. (define (main)
  61.     (if (option-ref options 'help #f)
  62.          (usage 0))
  63.     (if (option-ref options 'version #f)
  64.          (print-version))
  65.     (if (= (length args) 1)
  66.          (let ((request-uri (string->uri (car args))))
  67.              (if request-uri
  68.                  (http-request request-uri)
  69.                  (format (current-error-port) "Illegal url: ~a~%" (car args))))
  70.          (begin
  71.              (format (current-error-port) "Wrong number of arguments~%")
  72.              (usage 1))))

  73. (main)

阅读(4920) | 评论(0) | 转发(0) |
0

上一篇:循环字符串问题

下一篇:Base-26数字算术

给主人留下些什么吧!~~