curl : 命令行里访问网页,网站,下载文件

[root@lxglinux ~]# curl www.apelearn.com  #这个命令之后,出现了www.apelearn.com网站的源码一大堆

[root@lxglinux ~]# curl -I !$  -I省略源码

curl -I www.apelearn.com

HTTP/1.1 200 OK

Server: nginx

Date: Wed, 27 Jan 2016 15:16:31 GMT

Content-Type: text/html

Connection: keep-alive

Vary: Accept-Encoding

X-Powered-By: PHP/5.3.27


上面注意到有一个200 OK ,其实常见的状态码还有200 301 302 404 403 502 503

-----------------------------------------------------------------------------------


[root@lxglinux ~]# curl -I www.lishiming.net  curl一下这个网站发现一个状态码301,意味着设置了跳转

HTTP/1.1 301 Moved Permanently 

Server: nginx/1.0.15

Date: Wed, 27 Jan 2016 15:23:44 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http://www.apelearn.com/bbs/forum.php #跳转到这个域名下。


[root@lxglinux ~]#


--------------------------------------------------------------------------------------


[root@lxglinux ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.1 www.qq.com

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

"/etc/hosts" 3L, 181C  

[root@lxglinux ~]# ping www.qq.com

PING www.qq.com (192.168.1.1) 56(84) bytes of data.

---------------------------------------------------------------------------


[root@lxglinux ~]# ping www.qq.com

PING www.qq.com (220.181.138.29) 56(84) bytes of data.

64 bytes from 220.181.138.29: icmp_seq=1 ttl=128 time=78.3 ms

64 bytes from 220.181.138.29: icmp_seq=2 ttl=128 time=77.5 ms

64 bytes from 220.181.138.29: icmp_seq=3 ttl=128 time=83.9 ms

64 bytes from 220.181.138.29: icmp_seq=4 ttl=128 time=87.4 ms

64 bytes from 220.181.138.29: icmp_seq=5 ttl=128 time=77.2 ms

64 bytes from 220.181.138.29: icmp_seq=6 ttl=128 time=77.6 ms

^Z

[4]+  Stopped                 ping www.qq.com

[root@lxglinux ~]#

--------------------------------------------------------------------------------

glinux ~]# curl -x220.181.138.29:80 www.qq.com -I  #通过-x命令即使配置文件修改了,也能ping到正确的地址,别忘了                                                   加I省略信息

HTTP/1.1 200 OK

Server: squid/3.4.3

Date: Wed, 27 Jan 2016 15:32:12 GMT

Content-Type: text/html; charset=GB2312

Connection: keep-alive

Vary: Accept-Encoding

Vary: Accept-Encoding

Expires: Wed, 27 Jan 2016 15:33:12 GMT

Cache-Control: max-age=60

Vary: Accept-Encoding

Vary: Accept-Encoding

X-Cache: HIT from beijing.qq.com

----------------------------------------------------------------------------------


[root@lxglinux ~]# curl -IV www.qq.com             #显示访问过程。

curl 7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2

Protocols: tftp ftp telnet dict ldap ldaps http file https ftps scp sftp

Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz

-----------------------------------------------------------------------------------


[root@lxglinux ~]# curl -Iv www.qq.com

* About to connect() to www.qq.com port 80 (#0)

*   Trying 220.181.138.29... connected

* Connected to www.qq.com (220.181.138.29) port 80 (#0)

> HEAD / HTTP/1.1

> User-Agent: curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2

> Host: www.qq.com

> Accept: */*

>

< HTTP/1.1 200 OK

HTTP/1.1 200 OK

< Server: squid/3.4.3

Server: squid/3.4.3

< Date: Wed, 27 Jan 2016 15:45:45 GMT

Date: Wed, 27 Jan 2016 15:45:45 GMT

< Content-Type: text/html; charset=GB2312

Content-Type: text/html; charset=GB2312

< Connection: keep-alive

Connection: keep-alive

< Vary: Accept-Encoding

Vary: Accept-Encoding

< Vary: Accept-Encoding

Vary: Accept-Encoding

< Expires: Wed, 27 Jan 2016 15:46:45 GMT

Expires: Wed, 27 Jan 2016 15:46:45 GMT

< Cache-Control: max-age=60

Cache-Control: max-age=60

< Vary: Accept-Encoding

Vary: Accept-Encoding

< Vary: Accept-Encoding

Vary: Accept-Encoding

< X-Cache: HIT from beijing.qq.com

X-Cache: HIT from beijing.qq.com

* no chunk, no close, no size. Assume close to signal end


<

* Closing connection #0



*********useragent 标示,每个网站都有它自己的标识。

-------------------------------------------------------------------------------


[root@lxglinux ~]# curl -u username:password http://www.qq.com     ^C  指定user访问网站

[root@lxglinux ~]# curl -u username:password http://www.qq.com -I      当然后面可以加-I,简略信息。

HTTP/1.1 200 OK

Server: squid/3.4.3

Date: Wed, 27 Jan 2016 15:52:13 GMT

Content-Type: text/html; charset=GB2312

Connection: keep-alive

Vary: Accept-Encoding

Vary: Accept-Encoding

Expires: Wed, 27 Jan 2016 15:53:13 GMT

Cache-Control: max-age=60

Vary: Accept-Encoding

Vary: Accept-Encoding

X-Cache: HIT from beijing.qq.com

---------------------------------------------------------------------------------

[root@lxglinux ~]# curl -O    http://passport.ixpub.net/data/avatar/016/72/81/39_avatar_middle.jpg   最后一节是默认名称,跟网页中的保持一致 -大O不能指定名称,重新命名。

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100  5725  100  5725    0     0   1060      0  0:00:05  0:00:05 --:--:-- 35559

[root@lxglinux ~]# ls 39_avatar_middle.jpg  

39_avatar_middle.jpg

[root@lxglinux ~]# curl -o xiazai  http://passport.ixpub.net/data/avatar/016/72/81/39_avatar_middle.jp #-小O的话可以指定重命名的名称。