2013년 4월 11일 목요일

curl 사용법

기본 사용
$ curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.kr/">here</A>.
</BODY></HTML>


 - 302 는 redirection 코드

자세한 내용 표시 : -v 옵션
$ curl -v www.google.com
* About to connect() to www.google.com port 80 (#0)
*   Trying 74.125.128.147...
* connected
* Connected to www.google.com (74.125.128.147) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: www.google.com
> Accept: */*
< HTTP/1.1 302 Found
< Location: http://www.google.co.kr/
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Set-Cookie: PREF=ID=772acf0ed672750a:FF=0:TM=1365725569:LM=1365725569:S=oN2I30J2yduSpsDA; expires=Sun, 12-Apr-2015 00:12:49 GMT; path=/; domain=.google.com
< Set-Cookie: NID=67=n0oJCc3IUnhapZm7IaK47FB9JpM81dAjqLA1ifC5yYPWBg6-g4K_ZcPJaZOD4t23yVK4EzPB7wpe0zsEEHT3hdTQbi9Q-s21LORDQEZRkuWdS2YUzaOw2OjzzWV76b6P; expires=Sat, 12-Oct-2013 00:12:49 GMT; path=/; domain=.google.com; HttpOnly
< P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
< Date: Fri, 12 Apr 2013 00:12:49 GMT
< Server: gws
< Content-Length: 221
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.kr/">here</A>.
</BODY></HTML>
* Connection #0 to host www.google.com left intact
* Closing connection #0

 - curl  이 작업하는 과정과 서버로 전송하는 내용 등을 자세하게 표시해 준다.

redirect 따라가기 : -L 옵션
$ curl -L www.google.com
<!doctype html><html itemscope="itemscope" itemtype="http://schema.org/WebPage"><head><meta itemprop="image" content="/images/google_favicon_128.png"><title>Google</title><script>(function(){
window.google={kEI:"jEhnUYS0HJCPiAf5jIGYCw",getEI:function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI},https:function()
 -- 생략 --

파일 다운로드 : -O (대문자 영문자 오) 옵션
$ curl -O -L www.google.com/index.html
$ ls
index.html

 - 파일 이름 지정 : -o 옵션
$ curl -O -L www.google.com/index.html -o google.html

HTTPS 사이트 접속
$ curl -L https://www.google.com:443
만약 실패한다면 클라이언트 PC 에 인증서가 없거나 해당 사이트의 인증서가 잘못된 것일 수 있다.
 - 인증을 무시하고 접근하려면 -k 옵션을 사용
$ curl -k -L https://www.google.com:443
 - 공개 인증서 다운로드 : http://curl.haxx.se/ca/ 에서 cacert.pem 파일을 다운로드
 - 다운로드 받은 인증서를 매개변수로 추가
$ curl --cacert cacert.pem https://www.google.com:443

데이터 전송 (post 방식)
옵션 종류 (모두 나열하지 않음)
  • --data : 입력한 매개변수 내용 그대로 전송
  • --data-binary : 입력한 매개변수 내용 그대로 전송 (binary)
  • --form : form 형식으로 전송
--data 와 --data-binary 매개변수
 - 매개변수에 "hello" 라고 넣어주면 문자그대로 hello 를 데이터 인식하고 전달한다.
 - 매개변수에 @ 를 앞에 붙이면 파일명으로 인식하고 파일을 찾아 파일 내용을 전달한다.
   > $ curl --data-binary @hello.txt www.google.com
   > 내용이 form 같은 형식 등으로 가공되지 않고 그대로 보내진다.

--form
$ curl --form greeting=hello www.google.com
 ==> form 형식으로 전송

팁: 마지막 받은 위치부터 받기
!) 서버에서 HTTP Range spec 을 지원해야 함
$ curl -L -O -C - $DOWNLOAD_URL
 - '-C -' 옵션을 추가

댓글 없음:

댓글 쓰기