New FAMILUG

The PyMiers

Thursday 7 March 2013

curl

Cài đặt
hvn@lappy: ~ () $ sudo apt-get install curl


Bắt đầu với "framework" khám phá các command-line tool của FAMILUG:

hvn@lappy: ~ () $ whatis curl
curl (1)             - transfer a URL
hvn@lappy: ~ () $ apt-cache show curl
...
Package: curl
Original-Maintainer: Ramakrishnan Muthukrishnan <rkrishnan@debian.org>
Version: 7.22.0-3ubuntu4
Description-en: Get a file from an HTTP, HTTPS or FTP server
 curl is a client to get files from servers using any of the supported
 protocols. The command is designed to work without user interaction
 or any kind of interactivity.
 .
 curl offers a busload of useful tricks like proxy .support, user
 authentication, FTP upload, HTTP post, file transfer resume and more.
Homepage: http://curl.haxx.se
... Ngoài ra còn nhiều thông tin giới thiêu/ định nghĩa curl khác, xem thêm ở cuối bài

bài viết này giới thiệu một vài cách sử dụng chương trình dòng lệnh curl để thực hiện một số HTTP method [1] đơn giản như GET, POST

1. Tải file
       -o, --output <file>
       -O, --remote-name

cú pháp:
curl link_đến_file -o tenfile.xxx
curl link_đến_file -O    # (Chữ o hoa) 
Ví dụ:
hvn@lappy: /tmp/test () $ curl http://l.yimg.com/us.yimg.com/i/mesg/emoticons7/8.gif -o love.gif
hvn@lappy: /tmp/test () $ curl http://l.yimg.com/us.yimg.com/i/mesg/emoticons7/8.gif -O
...
hvn@lappy: /tmp/test () $ ls -l
total 8
-rw-rw-r-- 1 hvn hvn 2323 Mar  6 23:54 8.gif
-rw-rw-r-- 1 hvn hvn 2323 Mar  6 23:53 love.gif

2. Khám phá / sử dụng HTTP
  -I, --head
              (HTTP/FTP/FILE)  Fetch the HTTP-header only! HTTP-servers feature the command HEAD which this uses to get nothing but the header of a document.

-X, --request <command>
              (HTTP) Specifies a custom request method to use when communicating with the HTTP server.

 -v, --verbose
              Makes the fetching more verbose/talkative. Mostly useful for debugging. A line starting with '>' means "header data"  sent  by  curl,  '<'  means "header data" received by curl that is hidden in normal cases, and a line starting with '*' means additional info provided by curl.

Ví dụ : thông tin HTTP-header các HTTP-server trả về

hvn@lappy: ~ () $ curl -I www.familug.org
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Expires: Wed, 06 Mar 2013 17:00:46 GMT
Date: Wed, 06 Mar 2013 17:00:46 GMT
Cache-Control: private, max-age=0
Last-Modified: Wed, 06 Mar 2013 17:00:31 GMT
ETag: "59a789f0-57dd-401f-8cb8-fc9358a68cd5"
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Length: 0
Server: GSE


hvn@lappy: ~ () $ curl -I dantri.com.vn
HTTP/1.1 200 OK
Cneonction: close
Date: Wed, 06 Mar 2013 17:03:43 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 88300


hvn@lappy: ~ () $ curl -I http://www.debian.org
HTTP/1.1 200 OK
Date: Wed, 06 Mar 2013 17:05:19 GMT
Server: Apache
Content-Location: index.en.html
Vary: negotiate,accept-language,Accept-Encoding
TCN: choice
Last-Modified: Wed, 06 Mar 2013 03:30:32 GMT
ETag: "38f2-4d739365cc600"
Accept-Ranges: bytes
Content-Length: 14578
Cache-Control: max-age=86400
Expires: Thu, 07 Mar 2013 17:05:19 GMT
Content-Type: text/html
Content-Language: en
Tương tác với http server, lấy trang index của www.familug.org , ghi ra màn hình (toàn là mã html + js - giống khi bạn view source của site)
hvn@lappy: ~ () $ curl -X GET www.familug.org
...

Kết hợp với plugin  Firebug của Firefox để lấy các thông tin khi gửi request, thử lấy link down 1 bài hát:

hvn@lappy: ~ () $ curl -X GET http://www.nhaccuatui.com/download/song/dTtgAP3tZFZE
{"error_message":"Success","data":{"stream_url":"http:\/\/s41.stream.nixcdn.com\/5396999cf8fb219f09a69048ad942348\/51376783\/NhacCuaTui793\/NgayXuaEmDen-AnhKhang_4dmq4_hq.mp3","is_charge":"false"},"error_code":0}

Nếu bạn  biết rằng phía dưới mọi hoạt động lướt web của bạn,  trình duyệt web gửi các HTTP request để tương tác với HTTP server, bạn hoàn toàn có thể dùng curl thay cho trình duyệt của mình! ta có thể sử dụng curl để gửi POST request đến HTTP Server, ví dụ, hoàn toàn có thể dùng curl để gửi đi 1 message vào ô chatbox ở bên phải trang này. Và nếu có tinh thần phá hoại hơn, bạn có thể đặt nó vào 1 vòng lặp vô hạn =.= (đừng dại dột, tớ sẽ ban IP :D ). Hãy tìm 1 cái chatbox khác mà chơi :D

Để gửi data kèm POST request, dùng option:
-d, --data <data>
              (HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has  filled  in an HTML form and presses the submit button.

NOTE: -X POST hay -XPOST đều như nhau cả.
Ví dụ:
hvn@lappy: ~ () $ curl -XPOST 'http://link_bí_mật' -d 'nme=HVN&pst=PostUsedCURLByHVN'

phần viết 1 chương trình phá hoại chatbox coi như bài tập của bài viết này :D

curl còn rất nhiều option khác mà tớ không nêu trong bài viết này, mở terminal lên và gõ: man curl


Wiki:
cURL is a computer software project providing a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997.

Trang chủ
curl is a command line tool for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks.

libcurl:  is a free client-side URL transfer library
cURL: is a command line tool for getting or sending files using URL syntax.

[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

No comments:

Post a Comment