
时间:2015-06-28 00:00 来源:IT猫扑网|http://www.itmop.com/ 作者:网管联盟 我要评论(0)
来源于CU的一个帖子,问如何查看web服务器信息,忽然想起自己好像也比较想知道,遂google之
[root@dbrg-2 ~]# curl -I www.baidu.com
HTTP/1.1 200 OK
Date: Wed, 29 Jul 2009 02:51:19 GMT
Server: BWS/1.0
Content-Length: 3509
Content-Type: text/html
Cache-Control: private
Expires: Wed, 29 Jul 2009 02:51:19 GMT
Set-Cookie: BAIDUID=0D5F54C0853B7C38D6BD3A3E5EA63C44:FG=1; expires=Wed, 29-Jul-39 02:51:19 GMT; path=/; domain=.baidu.com
P3P: CP=& OTI DSP COR IVA OUR IND COM &
[root@dbrg-2 ~]# wget -S --spider www.baidu.com
--10:51:34-- http://www.baidu.com/
=> `index.html
那么工具是实现了,如何用c来实现呢... 其实也很简单,我刚开始还走了点弯路想的是去查看wget or curl的源码,奈何那种代码相互之间依赖性太强实在不易阅读,于是自己用wireshark抓包, so easy!!!!有兴趣的自己去抓下
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define HTTPPORT 80
char* head =
&HEAD / HTTP/1.0rn&
&Accept: */*rn&
&User-Agent: Wget/1.10.2 (Red Hat modified)rn&
&Host:127.0.0.1rn&
&Connection: Keep-Alivernrn&;
int connect_URL(char *domain,int port)
{
int sock;
struct hostent * host;
struct sockaddr_in server;
host = gethostbyname(domain);
if (host == NULL)
{
printf(&gethostbyname errorn&);
return -2;
}
#p#副标题#e#
sock = socket(AF_INET,SOCK_STREAM,0);
if (sock < 0)
{
printf(&invalid socketn&);
return -1;
}
memset(&server,0,sizeof(struct sockaddr_in));
memcpy(&server.sin_addr,host->h_addr_list[0],host->h_length);
server.sin_family = AF_INET;
server.sin_port = htons(port);
return (connect(sock,(struct sockaddr *)&server,sizeof(struct sockaddr)) <0) ? -1 : sock;
}
int main()
{
int sock;
int ret;
char buf[100];
char *domain = &127.0.0.1&;
FILE* fp = fopen(&test&,&w+&);
if(NULL == fp){
printf(&can't open stockcode file!n&);
return -1;
}
sock = connect_URL(domain,HTTPPORT);
if (sock <0){
printf(&connetc errn&);
return -1;
}
send(sock,head,strlen(head),0);
while(1)
{
if((ret=recv(sock,buf,100-1,0))<1)
break;
buf[ret]=' ';
printf(&%s&, buf);
fprintf(fp,&%s&,buf); //save http data
}
fclose(fp);
close(sock);
//printf(&bye!n&);
return 0;
}
关键词标签:wget,curl,web服务器
相关阅读 安装红帽子RedHat Linux9.0操作系统教程 Tomcat9.0如何安装_Tomcat9.0环境变量配置方法 多种操作系统NTP客户端配置 Linux操作系统修改IP Linux实现SCSI硬盘热插拔及在线识别 Linux下用CDMA modem拨号上网
热门文章
安装红帽子RedHat Linux9.0操作系统教程
Linux服务器:设计高性能网站架构-LLMP
使用Clonezilla迁移到虚拟Linux环境
Linux上的MRTG流量监控中心
Linux 双网卡绑定一个IP原理及实现
linux和windows等系统远程控制ubuntu桌面
人气排行 Linux下获取CPUID、硬盘序列号与MAC地址 dmidecode命令查看内存型号 linux tc实现ip流量限制 安装红帽子RedHat Linux9.0操作系统教程 linux下解压rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 关机、重启、注销 命令 查看linux服务器硬盘IO读写负载 linux命令行浏览器的使用方法 Linux NFS服务固定端口及防火墙配置 U盘安装Ubuntu 10.04 Linux清除用户登录记录和命令历史方法
查看所有0条评论>>