江蘇常州青之峰做網(wǎng)站營銷頁面
提示:文章寫完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔
文章目錄
- 前言
- 一、場景
- 二、介紹
- 1.測試端口
- 2.訪問百度
- 3. 簡單的爬蟲
前言
最近telnet
命令用的比較多,所以記錄一下。
一、場景
ping
應(yīng)該是大家最常用的命令,可以測試節(jié)點(diǎn)之間通不通。但是我們?nèi)绻霚y試節(jié)點(diǎn)之間的指定端口通不通呢?
而且最近遇到了一些場景,就是配置端口映射的ip
,是不能ping
通的。但是可以用ssh
指定端口連接。
那么這個(gè)時(shí)候我們可以用telnet
測試指定節(jié)點(diǎn)的指定端口通不通。
二、介紹
1.測試端口
telnet ip port
2.訪問百度
其實(shí)通過這個(gè)例子可以更好的理解http
協(xié)議。 依稀記得當(dāng)年看《計(jì)算機(jī)網(wǎng)絡(luò):自頂向下方法》的時(shí)光。其實(shí)課本上描述的很詳細(xì)了,什么定義GET
,Host
字段,然后實(shí)操呢?實(shí)操僅僅是考試嗎?會(huì)影響我訪問網(wǎng)頁嗎?
但其實(shí)一切都是可以實(shí)操的。計(jì)算機(jī)沒有黑魔法,我們可以通過知識(shí)去理解這個(gè)真實(shí)的世界。
ping
獲取百度地址
? ping www.baidu.com
PING www.a.shifen.com (180.101.50.188) 56(84) bytes of data.
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=1 ttl=51 time=14.7 ms
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=2 ttl=51 time=12.0 ms
^C64 bytes from 180.101.50.188: icmp_seq=3 ttl=51 time=11.1 ms--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2175ms
rtt min/avg/max/mdev = 11.125/12.580/14.652/1.504 ms
telnet
發(fā)送http
請(qǐng)求
? telnet 180.101.50.188 80
Trying 180.101.50.188...
Connected to 180.101.50.188.
Escape character is '^]'.
GET / HTTP/1.1
Connection: close
Host: www.baidu.comHTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: no-cache
Content-Length: 9508
Content-Type: text/html
Date: Fri, 02 Feb 2024 05:43:38 GMT
P3p: CP=" OTI DSP COR IVA OUR IND COM "
P3p: CP=" OTI DSP COR IVA OUR IND COM "
Pragma: no-cache
Server: BWS/1.1
Set-Cookie: BAIDUID=CB255C6E0972913C1C5DD40F2FA3A92C:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=CB255C6E0972913C1C5DD40F2FA3A92C; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1706852618; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BAIDUID=CB255C6E0972913C9B77469AFEF54677:FG=1; max-age=31536000; expires=Sat, 01-Feb-25 05:43:38 GMT; domain=.baidu.com; path=/; version=1; comment=bd
Traceid: 1706852618059642266612178835182056953725
Vary: Accept-Encoding
X-Ua-Compatible: IE=Edge,chrome=1
X-Xss-Protection: 1;mode=block
Connection: close<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="referrer"><meta name="description" content="全球領(lǐng)先的中文搜索引擎、致力于讓網(wǎng)民更便捷地獲取信息,找到所求。百度超過千億的中文網(wǎng)頁數(shù)據(jù)庫,可以瞬間找到相關(guān)的搜索結(jié)果。"><link rel="shortcut icon" href="//www.baidu.com/favicon.ico" type="image/x-icon"><link rel="search" type="application/opensearchdescription+xml" href="//www.baidu.com/content-search.xml" title="百度搜索"><title>百度一下,你就知道</title>
一切沒有黑魔法,都是約定好的文本。課本里的知識(shí)在真實(shí)世界里得到了映射。
3. 簡單的爬蟲
既然telnet
可以訪問百度,那么telnet
是怎么訪問百度的?好像只是一些簡單的讀寫操作?所以我們能不能用linux
最基礎(chǔ)的系統(tǒng)調(diào)用(syscall
)實(shí)現(xiàn)訪問網(wǎng)頁?這是不是爬蟲的第一步呢?
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>#include <string>
#include <iostream>int main() {std::string domain("www.baidu.com");hostent* host = gethostbyname(domain.c_str());std::string hname(host->h_name);std::cout << hname << std::endl;for (int i = 0; host->h_addr_list[i]; ++i) {struct in_addr* addr = (struct in_addr*) host->h_addr_list[i];std::string ip(inet_ntoa(*addr));std::cout << ip << std::endl;}int client_socket = socket(AF_INET, SOCK_STREAM, 0);if (client_socket == -1) {std::cout << "Socket create failed" << std::endl;return -1;}struct sockaddr_in server_addr;server_addr.sin_family = AF_INET;server_addr.sin_addr.s_addr = *(in_addr_t *)host->h_addr_list[0];server_addr.sin_port = htons(80);int err = connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));if (err < 0 ) {std::cout << "error " << err << std::endl;return -1;}std::string httptext;httptext.append("GET / HTTP/1.1\r\n").append("Connection: close\r\n").append("Host: www.baidu.com\r\n").append("\r\n");ssize_t ret = write(client_socket, httptext.c_str(), httptext.size());if (ret != httptext.size()) {std::cout << "error: ret != httptext.size()" << std::endl;return -1;} else {std::cout << "success write" << std::endl;}char buffer[1024] = {0};ssize_t bytes_received;while(bytes_received = read(client_socket, buffer, sizeof(buffer))) {std::cout << buffer << std::endl;}return 0;
}
編譯,執(zhí)行~