zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    1

    http://curl.haxx.se/

    http://curl.haxx.se/docs/httpscripting.html

    curl is an open source command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more.

    What's curl used for?

    curl is used in command lines or scripts to transfer data. It is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the internet transfer backbone for thousands of software applications totally affecting more than one billion users.

    Who makes curl?

    curl is free and open software that compiles and runs under a wide variety of operating systems. curl exists thanks to thousands of contributors.

    What's the latest curl?

    The most recent stable version is 7.46.0, released on 2nd of December 2015. Currently, 88 of the listeddownloads are of the latest version.

    Where's the code?

    Check out the latest source code from github.

    1

     Linux curl使用简单介绍

    Curl是Linux下一个很强大的http命令行工具,其功能十分强大。

    1) 二话不说,先从这里开始吧!

    $ curl http://www.linuxidc.com

    回车之后,www.linuxidc.com 的html就稀里哗啦地显示在屏幕上了    ~

    2) 嗯,要想把读过来页面存下来,是不是要这样呢?

    $ curl http://www.linuxidc.com > page.html

    当然可以,但不用这么麻烦的!

    用curl的内置option就好,存下http的结果,用这个option: -o

    $ curl -o page.html http://www.linuxidc.com

    这样,你就可以看到屏幕上出现一个下载页面进度指示。等进展到100%,自然就 OK咯

    3) 什么什么?!访问不到?肯定是你的proxy没有设定了。

    使用curl的时候,用这个option可以指定http访问所使用的proxy服务器及其端口: -x

    $ curl -x 123.45.67.89:1080 -o page.html http://www.linuxidc.com

    4) 访问有些网站的时候比较讨厌,他使用cookie来记录session信息。

    像IE/NN这样的浏览器,当然可以轻易处理cookie信息,但我们的curl呢?.....

    我们来学习这个option: -D <— 这个是把http的response里面的cookie信息存到一个特别的文件中去

    $ curl -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://www.linuxidc.com

    这样,当页面被存到page.html的同时,cookie信息也被存到了cookie0001.txt里面了

    5)那么,下一次访问的时候,如何继续使用上次留下的cookie信息呢?要知道,很多网站都是靠监视你的cookie信息,来判断你是不是不按规矩访问他们的网站的。

    这次我们使用这个option来把上次的cookie信息追加到http request里面去: -b

    $ curl -x 123.45.67.89:1080 -o page1.html -D cookie0002.txt -b cookie0001.txt http://www.linuxidc.com

    这样,我们就可以几乎模拟所有的IE操作,去访问网页了!

    6)稍微等等    ~我好像忘记什么了    ~

    对了!是浏览器信息

    有些讨厌的网站总要我们使用某些特定的浏览器去访问他们,有时候更过分的是,还要使用某些特定的版本     NND,哪里有时间为了它去找这些怪异的浏览器呢!?

    好在curl给我们提供了一个有用的option,可以让我们随意指定自己这次访问所宣称的自己的浏览器信息: -A

    $ curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://www.linuxidc.com

    这样,服务器端接到访问的要求,会认为你是一个运行在Windows 2000上的 IE6.0,嘿嘿嘿,其实也许你用的是苹果机呢!

    而"Mozilla/4.73 [en] (X11; U; Linux 2.2; 15 i686"则可以告诉对方你是一台 PC上跑着的Linux,用的是Netscape 4.73,呵呵呵

    7)另外一个服务器端常用的限制方法,就是检查http访问的referer。比如你先访问首页,再访问里面所指定的下载页,这第二次访问的 referer地址就是第一次访问成功后的页面地址。这样,服务器端只要发现对下载页面某次访问的referer地址不是首页的地址,就可以断定那是个盗连了    ~

    讨厌讨厌 ~我就是要盗连    ~!!

    幸好curl给我们提供了设定referer的option: -e

    $ curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -x 123.45.67.89:1080 -e "mail.linuxidc.com" -o page.html -D cookie0001.txt http://www.linuxidc.com

    这样,就可以骗对方的服务器,你是从mail.linuxidc.com点击某个链接过来的了,呵呵呵

    8)写着写着发现漏掉什么重要的东西了!——- 利用curl 下载文件

    刚才讲过了,下载页面到一个文件里,可以使用 -o ,下载文件也是一样。比如,

    $ curl -o 1.jpg http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG

    这里教大家一个新的option: -O 大写的O,这么用:

    $ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG

    这样,就可以按照服务器上的文件名,自动存在本地了!

    再来一个更好用的。

    如果screen1.JPG以外还有screen2.JPG、screen3.JPG、....、screen10.JPG需要下载,难不成还要让我们写一个script来完成这些操作?

    不干!

    在curl里面,这么写就可以了:

    $ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen[1-10].JPG

    呵呵呵,厉害吧?! ~

    9)再来,我们继续讲解下载!

    $ curl -O http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG

    这样产生的下载,就是

    ~zzh/001.JPG

    ~zzh/002.JPG

    ...

    ~zzh/201.JPG

    ~nick/001.JPG

    ~nick/002.JPG

    ...

    ~nick/201.JPG

    够方便的了吧?哈哈哈

    咦?高兴得太早了。

    由于zzh/nick下的文件名都是001,002...,201,下载下来的文件重名,后面的把前面的文件都给覆盖掉了 ~

    没关系,我们还有更狠的!

    $ curl -o #2_#1.jpg http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG

    —这是.....自定义文件名的下载? —对头,呵呵!

    这样,自定义出来下载下来的文件名,就变成了这样:原来: ~zzh/001.JPG —-> 下载后: 001-zzh.JPG 原来: ~nick/001.JPG —-> 下载后: 001-nick.JPG

    这样一来,就不怕文件重名啦,呵呵

    9)继续讲下载

    我们平时在windows平台上,flashget这样的工具可以帮我们分块并行下载,还可以断线续传。curl在这些方面也不输给谁,嘿嘿

    比如我们下载screen1.JPG中,突然掉线了,我们就可以这样开始续传

    $ curl -c -O http://cgi2.tky.3wb.ne.jp/~zzh/screen1.JPG

    当然,你不要拿个flashget下载了一半的文件来糊弄我    别的下载软件的半截文件可不一定能用哦 ~

    分块下载,我们使用这个option就可以了: -r

    举例说明

    比如我们有一个http://cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 要下载(赵老师的电话朗诵 :D )我们就可以用这样的命令:

    $ curl -r 0-10240 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &

    $ curl -r 10241-20480 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &

    $ curl -r 20481-40960 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &

    $ curl -r 40961- -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3

    这样就可以分块下载啦。不过你需要自己把这些破碎的文件合并起来如果你用UNIX或苹果,用 cat zhao.part* > zhao.mp3就可以如果用的是Windows,用copy /b 来解决吧,呵呵

    上面讲的都是http协议的下载,其实ftp也一样可以用。用法嘛,

    $ curl -u name:passwd ftp://ip:port/path/file

    或者大家熟悉的

    $ curl ftp://name:passwd@ip:port/path/file

    10) 说完了下载,接下来自然该讲上传咯上传的option是 -T

    比如我们向ftp传一个文件:

    $ curl -T localfile -u name:passwd ftp://upload_site:port/path/

    当然,向http服务器上传文件也可以比如

    $ curl -T localfile http://cgi2.tky.3web.ne.jp/~zzh/abc.cgi

    注意,这时候,使用的协议是HTTP的PUT method

    刚才说到PUT,嘿嘿,自然让老服想起来了其他几种methos还没讲呢! GET和POST都不能忘哦。

    http提交一个表单,比较常用的是POST模式和GET模式

    GET模式什么option都不用,只需要把变量写在url里面就可以了比如:

    $ curl http://www.linuxidc.com/login.cgi?user=nickwolfe&password=12345

    而POST模式的option则是 -d

    比如,

    $ curl -d "user=nickwolfe&password=12345" http://www.linuxidc.com/login.cgi

    就相当于向这个站点发出一次登陆申请    ~

    到底该用GET模式还是POST模式,要看对面服务器的程序设定。

    一点需要注意的是,POST模式下的文件上的文件上传,比如

    <form method="POST" enctype="multipar/form-data" action="http://cgi2.tky.3web.ne.jp/~zzh/up_file.cgi">

    <input type=file name=upload>

    <input type=submit name=nick value="go">

    </form>

    这样一个HTTP表单,我们要用curl进行模拟,就该是这样的语法:

    $ curl -F upload=@localfile -F nick=go http://cgi2.tky.3web.ne.jp/~zzh/up_file.cgi

    罗罗嗦嗦讲了这么多,其实curl还有很多很多技巧和用法比如 https的时候使用本地证书,就可以这样

    $ curl -E localcert.pem https://remote_server

    再比如,你还可以用curl通过dict协议去查字典    ~

    $ curl dict://dict.org/d:computer

    source: http://www.linuxidc.com/Linux/2008-01/10891.htm

    1

    curl网站开发指南

    curl是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在"标准输出"(stdout)上面。

    它支持多种协议,下面举例讲解如何将它用于网站开发。

    一、查看网页源码

    直接在curl命令后加上网址,就可以看到网页源码。我们以网址www.sina.com为例(选择该网址,主要因为它的网页代码较短):

      $ curl www.sina.com

      <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
      <html><head>
      <title>301 Moved Permanently</title>
      </head><body>
      <h1>Moved Permanently</h1>
      <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
      </body></html>

    如果要把这个网页保存下来,可以使用`-o`参数,这就相当于使用wget命令了。

      $ curl -o [文件名] www.sina.com

    二、自动跳转

    有的网址是自动跳转的。使用`-L`参数,curl就会跳转到新的网址。

      $ curl -L www.sina.com

    键入上面的命令,结果就自动跳转为www.sina.com.cn。

    三、显示头信息

    `-i`参数可以显示http response的头信息,连同网页代码一起。

      $ curl -i www.sina.com

      HTTP/1.0 301 Moved Permanently
      Date: Sat, 03 Sep 2011 23:44:10 GMT
      Server: Apache/2.0.54 (Unix)
      Location: http://www.sina.com.cn/
      Cache-Control: max-age=3600
      Expires: Sun, 04 Sep 2011 00:44:10 GMT
      Vary: Accept-Encoding
      Content-Length: 231
      Content-Type: text/html; charset=iso-8859-1
      Age: 3239
      X-Cache: HIT from sh201-9.sina.com.cn
      Connection: close

      <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
      <html><head>
      <title>301 Moved Permanently</title>
      </head><body>
      <h1>Moved Permanently</h1>
      <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
      </body></html>

    `-I`参数则是只显示http response的头信息。

    四、显示通信过程

    `-v`参数可以显示一次http通信的整个过程,包括端口连接和http request头信息。

      $ curl -v www.sina.com

      * About to connect() to www.sina.com port 80 (#0)
      * Trying 61.172.201.195... connected
      * Connected to www.sina.com (61.172.201.195) port 80 (#0)
      > GET / HTTP/1.1
      > User-Agent: curl/7.21.3 (i686-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
      > Host: www.sina.com
      > Accept: */*
      > 
      * HTTP 1.0, assume close after body
      < HTTP/1.0 301 Moved Permanently
      < Date: Sun, 04 Sep 2011 00:42:39 GMT
      < Server: Apache/2.0.54 (Unix)
      < Location: http://www.sina.com.cn/
      < Cache-Control: max-age=3600
      < Expires: Sun, 04 Sep 2011 01:42:39 GMT
      < Vary: Accept-Encoding
      < Content-Length: 231
      < Content-Type: text/html; charset=iso-8859-1
      < X-Cache: MISS from sh201-19.sina.com.cn
      < Connection: close
      < 
      <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
      <html><head>
      <title>301 Moved Permanently</title>
      </head><body>
      <h1>Moved Permanently</h1>
      <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
      </body></html>
      * Closing connection #0

    如果你觉得上面的信息还不够,那么下面的命令可以查看更详细的通信过程。

      $ curl --trace output.txt www.sina.com

    或者

      $ curl --trace-ascii output.txt www.sina.com

    运行后,请打开output.txt文件查看。

    五、发送表单信息

    发送表单信息有GET和POST两种方法。GET方法相对简单,只要把数据附在网址后面就行。

      $ curl example.com/form.cgi?data=xxx

    POST方法必须把数据和网址分开,curl就要用到--data参数。

      $ curl -X POST --data "data=xxx" example.com/form.cgi

    如果你的数据没有经过表单编码,还可以让curl为你编码,参数是`--data-urlencode`。

      $ curl -X POST--data-urlencode "date=April 1" example.com/form.cgi

    六、HTTP动词

    curl默认的HTTP动词是GET,使用`-X`参数可以支持其他动词。

      $ curl -X POST www.example.com

      $ curl -X DELETE www.example.com

    七、文件上传

    假定文件上传的表单是下面这样:

      <form method="POST" enctype='multipart/form-data' action="upload.cgi">
        <input type=file name=upload>
        <input type=submit name=press value="OK">
      </form>

    你可以用curl这样上传文件:

      $ curl --form upload=@localfilename --form press=OK [URL]

    八、Referer字段

    有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的。

      $ curl --referer http://www.example.com http://www.example.com

    九、User Agent字段

    这个字段是用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同设备,返回不同格式的网页,比如手机版和桌面版。

    iPhone4的User Agent是

      Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7

    curl可以这样模拟:

      $ curl --user-agent "[User Agent]" [URL]

    十、cookie

    使用`--cookie`参数,可以让curl发送cookie。

      $ curl --cookie "name=xxx" www.example.com

    至于具体的cookie的值,可以从http response头信息的`Set-Cookie`字段中得到。

    `-c cookie-file`可以保存服务器返回的cookie到文件,`-b cookie-file`可以使用这个文件作为cookie信息,进行后续的请求。

      $ curl -c cookies http://example.com
      $ curl -b cookies http://example.com

    十一、增加头信息

    有时需要在http request之中,自行增加一个头信息。`--header`参数就可以起到这个作用。

      $ curl --header "Content-Type:application/json" http://example.com

    十二、HTTP认证

    有些网域需要HTTP认证,这时curl需要用到`--user`参数。

      $ curl --user name:password example.com

    【参考资料】

      * Using cURL to automate HTTP jobs

      * 教你学用CURL

      * 9 uses for cURL worth knowing

    (完)

    http://www.ruanyifeng.com/blog/2011/09/curl.html 

    1

    CURL   (CommandLine Uniform Resource Locator)  简易教程!
    http://curl.haxx.se/docs/httpscripting.html
    https://httpkit.com/resources/HTTP-from-the-Command-Line/
    https://github.com/xgqfrms/curl
    http://php.net/manual/zh/book.curl.php
    http://www.linuxidc.com/Linux/2008-01/10891.htm
    http://curl.haxx.se/
    http://baike.baidu.com/item/curl
    http://www.runoob.com/php/php-ref-curl.html
    http://www.runoob.com/http/http-messages.html
    http://www.runoob.com/mongodb/mongodb-linux-install.html
    http://www.runoob.com/w3cnote/php-remote-filesize.html
    终端 ctrl + l 清屏相当于命令clear
    https://github.com/bagder/curl

    1

    xxxxxxxxxxxxxxxxx

    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  • 相关阅读:
    【NOIp】NOIp2015
    【题解】Luogu P2899 [USACO008JAN] 手机网络 树形dp
    「EZEC-4.5」走方格
    ZJOI2013 K大数查询
    CF765F Souvenirs
    [Vani有约会] 雨天的尾巴 /【模板】线段树合并
    中国剩余定理(CRT / EXCRT)
    洛谷 P1516 青蛙的约会
    CQOI2009 叶子的染色
    CSP2019 Emiya 家今天的饭
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/5070935.html
Copyright © 2011-2022 走看看