Command Line curl tutorial

Source: Internet
Author: User
Command Line curl tutorial

Command Line curl tutorial

Curl? Well, it's a long time ~~~~

This is already

Ibm aix toolbox

Is one of the command line tools.

1) Let's not talk about it. Let's start from here!


Curl http://www.yahoo.com

After you press enter, the HTML of www.yahoo.com is displayed on the screen ~~~~~

2) Well, is this the case if I want to save the read pages?


Curl http://www.yahoo.com> page.html

Yes, of course, but you don't have to worry about it!


Use the built-in option of curl to save the HTTP result. Use this option:-o


Http://www.yahoo.com curl-O page.html

In this way, you can see a download page progress indicator on the screen. When the Progress reaches 100%, it will naturally be OK.

3) What ?! Cannot access? It must be that your proxy has not been set.


When using curl, you can use this option to specify the proxy server used for HTTP access and its port:-x


Curl-x 123.45.67.89: 1080-O page.html http://www.yahoo.com

4) it is annoying to visit some websites. He uses cookies to record session information.


Browsers like IE/nn can easily process cookie information, but what about our curl ?.....


Let's learn this option:-D <-- this is to store the cookie information in the HTTP response to a special file.


Curl-x 123.45.67.89: 1080-O page.html-D cookie0001.txt http://www.yahoo.com

In this case, when the page is stored in the same page as page.html, cookieinformation is also stored in cookie0001.txt.

5) So how can I continue to use the cookie information left by the last visit? You know, many websites rely on monitoring your cookie information,


To determine if you do not access their website according to the rules.


This time, we use this option to append the last cookie information to the HTTP request:-B


Curl-x FIG: 1080-O page1.html-D cookie0002.txt-B cookie0001.txt http://www.yahoo.com

In this way, we can simulate almost all the IE operations to access the webpage!

6) Wait a moment ~~~~~ I seem to have forgotten something ~~~~~


Right! Browser Information ~~~~

Some annoying websites always use some specific browsers to access them. Sometimes, more importantly, some specific versions must be used ~~~~


Nnd, where can I find these weird browsers for it !?

Fortunately, curl provides us with a useful option, which allows us to randomly specify the browser information we declared during this access:-


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.yahoo.com

In this way, when the server receives an access request, it will think that you are an ie6.0 running on Windows 2000. Hey, maybe you are using an apple!

However, "Mozilla/4.73 [En] (X11; U; Linux 2.2; 15 i686" tells the other party that you are running Linux on a PC and use Netscape 4.73.

7) Another common restriction method on the server side is to check the Referer for HTTP access. For example, if you first access the homepage and then access the specified download page, the Referer address for the second visit is the address of the page after the first successful access. In this way, as long as the server finds that the Referer address accessed for a certain download page is not the address of the home page, it can be determined that the Referer address is a stolen connection ~~~~~

Hate ~~~ I just want to steal the connection ~~~~~!!


Fortunately, curl provides us with the option to set Referer:-e


Curl-a "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"-x 123.45.67.89: 1080-e "mail.yahoo.com"-O page.html-D cookie0001.txt http://www.yahoo.com

In this way, you can lie to the other server. You clicked a link from mail.yahoo.com.

8) It says something is missing! ----- Use curl to download an object

As mentioned earlier, you can use-O to download a file from the download page.


For example,


Curl-O 1.jpg http://cgi2.tky.3web.ne.jp /~ Zzh/screen1.jpg

Here we will teach you a new option:-o


The uppercase o is used as follows:


Curl-O http://cgi2.tky.3web.ne.jp /~ Zzh/screen1.jpg

In this way, the file name on the server is automatically stored locally!

Another one is better.


If screen1.jpg, screen2.jpg, screen3.jpg,..., and screen10.jpg need to be downloaded, can we write a script to complete these operations?


Don't do it!


In curl, you can write it like this:


Curl-O http://cgi2.tky.3web.ne.jp /~ Zzh/screen [1-10]. jpg

Hehao, amazing ?!~~~

9) Let's continue to explain the download process!


Curl-O http://cgi2.tky.3web.ne.jp /~ {Zzh, Nick}/[001-201]. jpg

This download is


~ Zzh/001.jpg


~ Zzh/002.jpg


...


~ Zzh/201.jpg


~ Nick/001.jpg


~ Nick/002.jpg


...


~ Nick/201.jpg

Is it convenient enough? Hahaha

Why? I'm so happy.


Because the file names under zzh/Nick are both 001,002... and 201, the downloaded file has the same name, and the previous file is overwritten by the following ~~~

It doesn't matter. We still have something worse!


Curl-O %2_%1.jpg http://cgi2.tky.3web.ne.jp /~ {Zzh, Nick}/[001-201]. jpg

-- This is the download of the custom file name?


-- Right, huh, huh!

#1 refers to the variable {zzh, Nick}. The first value is zzh and the second value is Nick.


#2 represents the variable, which is the variable part of the second paragraph --- [001-201]. The value is increased from 001 to 201 one by one.


In this way, the downloaded file name is changed to the following:


Original :~ Zzh/001.jpg ---> download: 001-zzh.jpg


Original :~ Nick/001.jpg ---> download: 001-nick.jpg

In this way, you are not afraid of duplicate names of files.

9) continue to download


On Windows, we usually use tools like flashget to help us download data in parallel in multiple parts and resume data transmission.


Curl won't lose to anyone in these aspects.

For example, if we download screen1.jpg and suddenly drop the line, we can resume the download.


Curl-c-o http://cgi2.tky.3wb.ne.jp /~ Zzh/screen1.jpg

Of course, you should not use a flash get to download half of the files to confuse me ~~~~ Other Software downloading half-width files may not be used ~~~

For multipart download, we can use this option:-R


Example


For example, we have a http://cgi2.tky.3web.ne.jp /~ Zzh/zhao1.mp3 to download (Instructor Zhao calls to recite)


We can use the following command:


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

In this way, you can download them in multiple parts.


However, you need to merge these broken files by yourself.


If you use Unix or apple, use Cat Zhao. Part *> zhao.pdf.


If Windows is used, use copy/B.

All of the above are HTTP protocol downloads. In fact, FTP can also be used.


Usage well,


Curl-u name: passwd ftp: // ip: Port/path/File

Or you are familiar


Curl ftp: // name: passwd @ IP: Port/path/File

10) after the download is completed, we will naturally talk about uploading.


The upload option is-t.

For example, we can upload a file to FTP:


Curl-T localfile-u name: passwd ftp: // upload_site: Port/path/

Of course, you can also upload files to the HTTP server.


For example


Curl-T localfile http://cgi2.tky.3web.ne.jp /~ Zzh/ABC. cgi

Note: At this time, the protocol used is the http put method.

Speaking of put, hey, naturally, it reminds the old server of the other methods that haven't been mentioned yet!


Neither get nor post can be forgotten.

When submitting a form over HTTP, the post mode and get mode are commonly used.

No option is required in get mode. You only need to write the variable in the URL.


For example:


Curl http://www.yahoo.com/login.cgi? User = nickwolfe & Password = 12345

The option in post mode is-D.


Curl-d "user = nickwolfe & Password = 12345" http://www.yahoo.com/login.cgi

This is equivalent to sending a login application to this site ~~~~~

Whether to use the get mode or post mode depends on the program settings of the opposite server.

Note that, in post mode, files are uploaded, such


<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>

To simulate such an HTTP form with curl, the syntax is as follows:


Curl-F upload = @ localfile-F Nick = go http://cgi2.tky.3web.ne.jp /~ Zzh/up_file.cgi

Luo luochao has talked so much about it. In fact, curl still has many skills and usage.


For example, if you use a local certificate for HTTPS


Curl-e localcert. pem https: // remote_server

For example, you can use curl to search the dictionary through the dict protocol ~~~~~


Curl dict: // dict.org/d:computer

Let's talk about it today. The crazy curl function requires you to explore it together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.