Curl使用详解(二)

xiaoxiao2021-02-28  48

7 UPLOADING上传

FTP / FTPS / SFTP / SCP Upload all data on stdin to a specified server: 上传标准输入的所有数据到指定服务器:

curl -T - ftp://ftp.upload.com/myfile

Upload data from a specified file, login with user and password: 使用用户名和密码,上传指定文件到服务器上(在URL中指定远程文件名称):

curl -T uploadfile -u user:passwd ftp://ftp.upload.com/myfile Upload a local file to the remote site, and use the local file name at the remote site too: 将本地文件上传到远程站点,远程服务器上的文件使用与本地文件相同的文件名: curl -T uploadfile -u user:passwd ftp://ftp.upload.com/

Upload a local file to get appended to the remote file: 将待上传的文件追加指定的远程文件之后:

curl -T localfile -a ftp://ftp.upload.com/remotefile

Curl also supports ftp upload through a proxy, but only if the proxy is configured to allow that kind of tunneling. If it does, you can run curl in a fashion similar to: curl也支持使用代理来完成文件上传到ftp,但是只有在代理服务器已经被配置为允许此类型的通道时才可以完成传输。如果代理服务器支持话,你就可以使用与下面命令类似的命令来完成文件的上传:

curl --proxytunnel -x proxy:port -T localfile ftp.upload.com

SMB / SMBS

curl -T file.txt -u "domain\username:passwd" smb://server.example.com/share/

HTTP Upload all data on stdin to a specified HTTP site: 上传标准输入的所有数据到指定的HTTP站点:

curl -T - http://www.upload.com/myfile

Note that the HTTP server must have been configured to accept PUT before this can be done successfully. 注意:HTTP服务器必须被事先配置为接受PUT, 上述命令才能被成功的执行。 For other ways to do HTTP data upload, see the POST section below. 对于HTTP数据上传的其他方式,请参见POST的相关内容。

8 VERBOSE / DEBUG

If curl fails where it isn’t supposed to, if the servers don’t let you in, if you can’t understand the responses: use the -v flag to get verbose fetching. Curl will output lots of info and what it sends and receives in order to let the user see all client-server interaction (but it won’t show you the actual data). 如果curl没有按照预想的方式运行,或者服务器拒绝你的访问,再或者你根本看不懂反馈信息,请使用参数-v 来获取完成的运行信息。curl将输出大量的发送和接收信息,来让用户查看所有的客户端和服务器的交互内容,但它并不会显示你的真实数据。

curl -v ftp://ftp.upload.com/

To get even more details and information on what curl does, try using the –trace or –trace-ascii options with a given file name to log to, like this: 如果还想从curl获得更为详尽的信息,请尝试使用参数–trace或者–trace-ascii 同时指定日志文件的名称:

curl --trace trace.txt www.haxx.se

9 DETAILED INFORMATION

Different protocols provide different ways of getting detailed information about specific files/documents. To get curl to show detailed information about a single file, you should use -I/–head option. It displays all available info on a single file for HTTP and FTP. The HTTP information is a lot more extensive. 不同的网络协议会提供获取特定文件或者文档详细信息不用的方法。想让curl显示单个文件的详细信息,你应该使用参数-I或者–head。使用该参数,执行结果将显示单个文档基于HTTP和FTP的所有可用信息。如果文档是基于HTTP的话,会有大量的信息可供查阅。 For HTTP, you can get the header information (the same as -I would show) shown before the data by using -i/–include. Curl understands the -D/–dump-header option when getting files from both FTP and HTTP, and it will then store the headers in the specified file. 基于HTTP, 你可以使用-i或者–include获取文档实际数据之前的header信息(这部分内容与使用-I参数得出的header信息应该是一致的)。当你从FTP或者HTTP上获取文件时,可以使用参数-D或者–dump-header,这样curl就可以将文件的header信息保存到指定的文件里了。 Store the HTTP headers in a separate file (headers.txt in the example): 下面举例说明,将HTTP的header存储到指定文件的方法(以header.txt文件为例):

curl –dump-header headers.txt curl.haxx.se Note that headers stored in a separate file can be very useful at a later time if you want curl to use cookies sent by the server. More about that in the cookies section. 注意:将header信息存储到指定的文件中对于你想使用curl向服务器发送cookes是一种非常有用的方法。更多内容请参见cookies小节。

10 POST (HTTP)

It’s easy to post data using curl. This is done using the -d option. The post data must be urlencoded. 使用curl提交数据(post data)是非常容易的, 具体的方法就是使用参数-d 。待提交的数据必须经过urlencoded。 Post a simple “name” and “phone” guestbook. 修改一个简单的由姓名和电话组成的留言本。

curl -d "name=Rafael Sagula&phone=3320780" \ http://www.where.com/guest.cgi

How to post a form with curl, lesson #1: 举例说明怎样通过使用curl来提交一个表单: Dig out all the tags in the form that you want to fill in.在你想要填写的表单中找出所有的\标签。 If there’s a “normal” post, you use -d to post. -d takes a full “post string”, which is in the format如果这是一次“常规”的提交,你可以使得参数-d来完成提交。-d将会使用如下格式提交一份完成的”提交字符(post string)”:

<variable1>=<data1>&<variable2>=<data2>&...

The ‘variable’ names are the names set with “name=” in the tags, and the data is the contents you want to fill in for the inputs. The data must be properly URL encoded. That means you replace space with + and that you replace weird letters with %XX where XX is the hexadecimal representation of the letter’s ASCII code. 变量(variable)是你在标签中找到的表单项,如“name”; 而=后面的数据(data)就是你要在该表单项下填写的内容,但是注意这里的数据必须是经过URL编码的。这就意味着,你需要将空格替换成+, 而将字母替换为%XX,其中的XX的以十六进制表示的ASCII编码的字符。 Example: 举例说明: page located at表单页面为:

<form action="post.cgi" method="post"> <input name=user size=10> <input name=pass type=password size=10> <input name=id type=hidden value="blablabla"> <input name=ding value="submit"> </form>

We want to enter user ‘foobar’ with password ‘12345’. 我们在这表单中想输入用户为:foobar, 密码为:123456。 To post to this, you enter a curl command line like:要提供这份表单,你需要输入下面的curl命令:

curl -d "user=foobar&pass=12345&id=blablabla&ding=submit" (continues) http://www.formpost.com/getthis/post.cgi

While -d uses the application/x-www-form-urlencoded mime-type, generally understood by CGI’s and similar, curl also supports the more capable multipart/form-data type. This latter type supports things like file upload. 当使用参数-d时mime类型application/x-www-form-urlencoded通常会被CGI或者类似的接口所接受,curl同时还支持功能更强的multipart/form-data类型,后者提供诸如文件上传的功能。 -F accepts parameters like -F “name=contents”. If you want the contents to be read from a file, use <@filename> as contents. When specifying a file, you can also specify the file content type by appending ‘;type=’ to the file name. You can also post the contents of several files in one field. -F接受-F “name=contents”形式的参数。如果你想从文件中读取表单的数据内容,可以使用<@filename>的形式来指定文件名。如果在指定文件的的同时,你还能指定文件的类型,那可以在文件名后面追加内容;type=< mime type>。你也在一个表单项中一次提交多份文件。 For example, the field name ‘coolfiles’ is used to send three files, with different content types using the following syntax:例如,使用名为’coolfiles’表单项用于提交三份类型不同的文件,应该使用如下的命令行格式:

curl -F "coolfiles=@fil1.gif;type=image/gif,fil2.txt,fil3.html" \ http://www.post.com/postit.cgi

If the content-type is not specified, curl will try to guess from the file extension (it only knows a few), or use the previously specified type (from an earlier file if several files are specified in a list) or else it will use the default type ‘application/octet-stream’. 如果文件的类型没有被指定,那么curl将会从文件的扩展名来判断文件的类型(其支持扩展名各类比较少),或者前面指定过文件类型的文件来判断(多个文件被指定在一个文件列表中),如果以上方法都不行的话,curl就会使用默认的类型application/octet-stream. Emulate a fill-in form with -F. Let’s say you fill in three fields in a form. One field is a file name which to post, one field is your name and one field is a file description. We want to post the file we have written named “cooltext.txt”. To let curl do the posting of this data instead of your favourite browser, you have to read the HTML source of the form page and find the names of the input fields. In our example, the input field names are ‘file’, ‘yourname’ and ‘filedescription’. 使用-F参数还填写一个有三个表单项的表单。三个表单项分别为:待提交的文件名、你的姓名和文件描述。待提交的文件名为cooltext.txt。使用curl来代替你常用的浏览器来提交表单,你必须从HTML源代码来读取表单,并找出待输入的表单项名称。在此例中,输入的表单项的名称分别为:file,yourname和filedscription

curl -F "file=@cooltext.txt" -F "yourname=Daniel" \ -F "filedescription=Cool text file with cool text inside" \ http://www.post.com/postit.cgi

To send two files in one post you can do it in two ways: 想要在一次提交操作中上传二份文件,你需要使用如下二种方式之一: 1.Send multiple files in a single “field” with a single field name:

curl -F "pictures=@dog.gif,cat.gif"

2.Send two fields with two field names: 使用二个表单项,分别填入一个文件:

curl -F "docpicture=@dog.gif" -F "catpicture=@cat.gif"

To send a field value literally without interpreting a leading ‘@’or ‘<’, or an embedded ‘;type=’, use –form-string instead of -F. This is recommended when the value is obtained from a user or some other unpredictable source. Under these circumstances, using -F instead of –form-string would allow a user to trick curl into uploading a file. 如果只是发送表单项的字面值(文件名),而不是发送实际文件,就不需要使用前缀@,<或者嵌入;type=, 这时推荐使用–form-string来代替-F。 特别是不能确定用户来源的情况下,使用-F来代替–form-string,将会使用户有机会欺骗curl来进行文件上传操作。

11 REFERRER

An HTTP request has the option to include information about which address referred it to the actual page. Curl allows you to specify the referrer to be used on the command line. It is especially useful to fool or trick stupid servers or CGI scripts that rely on that information being available or contain certain data. 一个HTTP请求应该包含跳转到当前有页面之前的页面的地址信息。curl允许你使用命令行来指定一个referrer。这对于欺骗依赖于特定信息的服务器程序或者CGI脚本来说是非常用的功能。

curl -e www.coolsite.com http://www.showme.com/

NOTE: The Referer: [sic] field is defined in the HTTP spec to be a full URL. 注意: referrer 属性可返回载入当前文档的文档的 URL。

12 USER AGENT

An HTTP request has the option to include information about the browser that generated the request. Curl allows it to be specified on the command line. It is especially useful to fool or trick stupid servers or CGI scripts that only accept certain browsers. 一个HTTP请求应该包含生成该请求的浏览器的相关信息。curl允许你使用命令行来指定一个浏览器。 这对于只接受特定浏览器请求的服务器或者CGI脚本来说是非常有用的功能。 Example: 举例说明:

curl -A 'Mozilla/3.0 (Win95; I)' http://www.nationsbank.com/

Other common strings: 其他表示浏览器的字符串如下:

'Mozilla/3.0 (Win95; I)' Netscape Version 3 for Windows 95 'Mozilla/3.04 (Win95; U)' Netscape Version 3 for Windows 95 'Mozilla/2.02 (OS/2; U)' Netscape Version 2 for OS/2 'Mozilla/4.04 [en] (X11; U; AIX 4.2; Nav)' NS for AIX 'Mozilla/4.05 [en] (X11; U; Linux 2.0.32 i586)' NS for Linux

Note that Internet Explorer tries hard to be compatible in every way: 表示Internet Explorer如下,但由于兼容性问题,不是每一次尝试都能成功:

'Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)' MSIE for W95

Mozilla is not the only possible User-Agent name: Mozilla以外的浏览器类型:

'Konqueror/1.0' KDE File Manager desktop client 'Lynx/2.7.1 libwww-FM/2.14' Lynx command line browser
转载请注明原文地址: https://www.6miu.com/read-1711284.html

最新回复(0)