時(shí)間:2015/6/28來(lái)源:IT貓撲網(wǎng)作者:網(wǎng)管聯(lián)盟我要評(píng)論(0)
來(lái)源于CU的一個(gè)帖子,問(wèn)如何查看web服務(wù)器信息,忽然想起自己好像也比較想知道,遂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-- https://www.baidu.com/
=> `index.html
那么工具是實(shí)現(xiàn)了,如何用c來(lái)實(shí)現(xiàn)呢... 其實(shí)也很簡(jiǎn)單,我剛開(kāi)始還走了點(diǎn)彎路想的是去查看wget or curl的源碼,奈何那種代碼相互之間依賴(lài)性太強(qiáng)實(shí)在不易閱讀,于是自己用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.0\r\n"
"Accept: */*\r\n"
"User-Agent: Wget/1.10.2 (Red Hat modified)\r\n"
"Host:127.0.0.1\r\n"
"Connection: Keep-Alive\r\n\r\n";
int connect_URL(char *domain,int port)
{
int sock;
struct hostent * host;
struct sockaddr_in server;
host = gethostbyname(domain);
if (host == NULL)
{
printf("gethostbyname error\n");
return -2;
}
#p#副標(biāo)題#e#
sock = socket(AF_INET,SOCK_STREAM,0);
if (sock < 0)
{
printf("invalid socket\n");
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 err\n");
return -1;
}
send(sock,head,strlen(head),0);
while(1)
{
if((ret=recv(sock,buf,100-1,0))<1)
break;
buf[ret]='\0';
printf("%s", buf);
fprintf(fp,"%s",buf); //save http data
}
fclose(fp);
close(sock);
//printf("bye!\n");
return 0;
}
關(guān)鍵詞標(biāo)簽:wget,curl,web服務(wù)器
相關(guān)閱讀
熱門(mén)文章 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程使用screen管理你的遠(yuǎn)程會(huì)話(huà)GNU/Linux安裝vmware如何登錄linux vps圖形界面 Linux遠(yuǎn)程桌面連
人氣排行 Linux下獲取CPUID、硬盤(pán)序列號(hào)與MAC地址linux tc實(shí)現(xiàn)ip流量限制dmidecode命令查看內(nèi)存型號(hào)linux下解壓rar文件安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程Ubuntu linux 關(guān)機(jī)、重啟、注銷(xiāo) 命令lcx.exe、nc.exe、sc.exe入侵中的使用方法查看linux服務(wù)器硬盤(pán)IO讀寫(xiě)負(fù)載