linux中shell curl命令获取http状态码

通过curl的-w参数我们可以自定义curl的输出,%{http_code}代表http状态码

 代码如下 复制代码

# curl -I -m 10 -o /dev/null -s -w %{http_code}  www.111cn.net

上面的输出是不含换行的,如果需要换行的话,加上\n

 代码如下 复制代码

# curl -I -m 10 -o /dev/null -s -w %{http_code}  www.111cn.net
200# curl -I -m 10 -o /dev/null -s -w %{http_code}"\n"  www.111cn.net
200

此处的换行方法不能正确执行。

linux curl命令

-a/--append 上传文件时,附加到目标文件

-A/--user-agent <string> 设置用户代理发送给服务器

- anyauth 可以使用“任何”身份验证方法

-b/--cookie <name=string/file> cookie字符串或文件读取位置

- basic 使用HTTP基本验证

-B/--use-ascii 使用ASCII /文本传输

-c/--cookie-jar <file> 操作结束后把cookie写入到这个文件中

-C/--continue-at <offset> 断点续转

-d/--data <data> HTTP POST方式传送数据

--data-ascii <data> 以ascii的方式post数据

--data-binary <data> 以二进制的方式post数据

--negotiate 使用HTTP身份验证

--digest 使用数字身份验证

--disable-eprt 禁止使用EPRT或LPRT

--disable-epsv 禁止使用EPSV

-D/--dump-header <file> 把header信息写入到该文件中

--egd-file <file> 为随机数据(SSL)设置EGD socket路径

--tcp-nodelay 使用TCP_NODELAY选项

-e/--referer 来源网址

-E/--cert <cert[:passwd]> 客户端证书文件和密码 (SSL)

--cert-type <type> 证书文件类型 (DER/PEM/ENG) (SSL)

--key <key> 私钥文件名 (SSL)

--key-type <type> 私钥文件类型 (DER/PEM/ENG) (SSL)

--pass <pass> 私钥密码 (SSL)

--engine <eng> 加密引擎使用 (SSL). "--engine list" for list

--cacert <file> CA证书 (SSL)

--capath <directory> CA目录 (made using c_rehash) to verify peer against (SSL)

--ciphers <list> SSL密码

--compressed 要求返回是压缩的形势 (using deflate or gzip)

--connect-timeout <seconds> 设置最大请求时间

--create-dirs 建立本地目录的目录层次结构

--crlf 上传是把LF转变成CRLF

-f/--fail 连接失败时不显示http错误

--ftp-create-dirs 如果远程目录不存在,创建远程目录

--ftp-method [multicwd/nocwd/singlecwd] 控制CWD的使用

--ftp-pasv 使用 PASV/EPSV 代替端口

--ftp-skip-pasv-ip 使用PASV的时候,忽略该IP地址

--ftp-ssl 尝试用 SSL/TLS 来进行ftp数据传输

--ftp-ssl-reqd 要求用 SSL/TLS 来进行ftp数据传输

-F/--form <name=content> 模拟http表单提交数据

-form-string <name=string> 模拟http表单提交数据

-g/--globoff 禁用网址序列和范围使用{}和[]

-G/--get 以get的方式来发送数据

-h/--help 帮助

-H/--header <line>自定义头信息传递给服务器

--ignore-content-length 忽略的HTTP头信息的长度

-i/--include 输出时包括protocol头信息

-I/--head 只显示文档信息

从文件中读取-j/--junk-session-cookies忽略会话Cookie

- 界面<interface>指定网络接口/地址使用

- krb4 <级别>启用与指定的安全级别krb4

-j/--junk-session-cookies 读取文件进忽略session cookie

--interface <interface> 使用指定网络接口/地址

--krb4 <level> 使用指定安全级别的krb4

-k/--insecure 允许不使用证书到SSL站点

-K/--config 指定的配置文件读取

-l/--list-only 列出ftp目录下的文件名称

--limit-rate <rate> 设置传输速度

--local-port<NUM> 强制使用本地端口号

-m/--max-time <seconds> 设置最大传输时间

--max-redirs <num> 设置最大读取的目录数

--max-filesize <bytes> 设置最大下载的文件总量

-M/--manual 显示全手动

-n/--netrc 从netrc文件中读取用户名和密码

--netrc-optional 使用 .netrc 或者 URL来覆盖-n

--ntlm 使用 HTTP NTLM 身份验证

-N/--no-buffer 禁用缓冲输出

-o/--output 把输出写到该文件中

-O/--remote-name 把输出写到该文件中,保留远程文件的文件名

-p/--proxytunnel 使用HTTP代理

--proxy-anyauth 选择任一代理身份验证方法

--proxy-basic 在代理上使用基本身份验证

--proxy-digest 在代理上使用数字身份验证

--proxy-ntlm 在代理上使用ntlm身份验证

-P/--ftp-port <address> 使用端口地址,而不是使用PASV

-Q/--quote <cmd>文件传输前,发送命令到服务器

-r/--range <range>检索来自HTTP/1.1或FTP服务器字节范围

--range-file 读取(SSL)的随机文件

-R/--remote-time 在本地生成文件时,保留远程文件时间

--retry <num> 传输出现问题时,重试的次数

--retry-delay <seconds> 传输出现问题时,设置重试间隔时间

--retry-max-time <seconds> 传输出现问题时,设置最大重试时间

-s/--silent静音模式。不输出任何东西

-S/--show-error 显示错误

--socks4 <host[:port]> 用socks4代理给定主机和端口

--socks5 <host[:port]> 用socks5代理给定主机和端口

--stderr <file>

-t/--telnet-option <OPT=val> Telnet选项设置

--trace <file> 对指定文件进行debug

--trace-ascii <file> Like --跟踪但没有hex输出

--trace-time 跟踪/详细输出时,添加时间戳

-T/--upload-file <file> 上传文件

--url <URL> Spet URL to work with

-u/--user <user[:password]>设置服务器的用户和密码

-U/--proxy-user <user[:password]>设置代理用户名和密码

-v/--verbose

-V/--version 显示版本信息

-w/--write-out [format]什么输出完成后

-x/--proxy <host[:port]>在给定的端口上使用HTTP代理

-X/--request <command>指定什么命令

-y/--speed-time 放弃限速所要的时间。默认为30

-Y/--speed-limit 停止传输速度的限制,速度时间'秒

-z/--time-cond 传送时间设置

-0/--http1.0 使用HTTP 1.0

-1/--tlsv1 使用TLSv1(SSL)

-2/--sslv2 使用SSLv2的(SSL)

-3/--sslv3 使用的SSLv3(SSL)

--3p-quote like -Q for the source URL for 3rd party transfer

--3p-url 使用url,进行第三方传送

--3p-user 使用用户名和密码,进行第三方传送

-4/--ipv4 使用IP4

-6/--ipv6 使用IP6

-#/--progress-bar 用进度条显示当前的传送状态

http状态码对应含义:https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81


curl_init — 初始化一个curl会话
curl_copy_handle — 拷贝一个curl连接资源的所有内容和参数
curl_errno — 返回一个包含当前会话错误信息的数字编号
curl_error — 返回一个包含当前会话错误信息的字符串
curl_exec — 执行一个curl会话
curl_getinfo — 获取一个curl连接资源句柄的信息
curl_multi_init — 初始化一个curl批处理句柄资源
curl_multi_add_handle — 向curl批处理会话中添加单独的curl句柄资源
curl_multi_close — 关闭一个批处理句柄资源
curl_multi_exec — 解析一个curl批处理句柄
curl_multi_getcontent — 返回获取的输出的文本流
curl_multi_info_read — 获取当前解析的curl的相关传输信息
curl_multi_remove_handle — 移除curl批处理句柄资源中的某个句柄资源
curl_multi_select — Get all the sockets associated with the cURL extension, which can then be "selected"
curl_setopt_array — 以数组的形式为一个curl设置会话参数
curl_setopt — 为一个curl设置会话参数
curl_version — 获取curl相关的版本信息
curl_close — 关闭一个curl会话


curl爬取过程中,会返回一个http_code,下面是他们的意义信息
$http_code["0"]="Unable to access";
$http_code["100"]="Continue";
$http_code["101"]="Switching Protocols";
$http_code["200"]=”OK”;
$http_code["201"]=”Created”;
$http_code["202"]=”Accepted”;
$http_code["203"]=”Non-Authoritative Information”;
$http_code["204"]=”No Content”;
$http_code["205"]=”Reset Content”;
$http_code["206"]=”Partial Content”;
$http_code["300"]=”Multiple Choices”;
$http_code["301"]=”Moved Permanently”;
$http_code["302"]=”Found”;
$http_code["303"]=”See Other”;
$http_code["304"]=”Not Modified”;
$http_code["305"]=”Use Proxy”;
$http_code["306"]=”(Unused)”;
$http_code["307"]=”Temporary Redirect”;
$http_code["400"]=”Bad Request”;
$http_code["401"]=”Unauthorized”;
$http_code["402"]=”Payment Required”;
$http_code["403"]=”Forbidden”;
$http_code["404"]=”Not Found”;
$http_code["405"]=”Method Not Allowed”;
$http_code["406"]=”Not Acceptable”;
$http_code["407"]=”Proxy Authentication Required”;
$http_code["408"]=”Request Timeout”;
$http_code["409"]=”Conflict”;
$http_code["410"]=”Gone”;
$http_code["411"]=”Length Required”;
$http_code["412"]=”Precondition Failed”;
$http_code["413"]=”Request Entity Too Large”;
$http_code["414"]=”Request-URI Too Long”;
$http_code["415"]=”Unsupported Media Type”;
$http_code["416"]=”Requested Range Not Satisfiable”;
$http_code["417"]=”Expectation Failed”;
$http_code["500"]=”Internal Server Error”;
$http_code["501"]=”Not Implemented”;
$http_code["502"]=”Bad Gateway”;
$http_code["503"]=”Service Unavailable”;
$http_code["504"]=”Gateway Timeout”;
$http_code["505"]=”HTTP Version Not Supported”;

发布了55 篇原创文章 · 获赞 29 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/ruibin_cao/article/details/98326141