模拟浏览器的实现
创建socket
建立连接
拼接请求协议
发送请求协议
接收服务器的响应内容
保存数据
关闭连接
import socket
# 创建socket
tcp_client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 建立连接
addr_server = ('42.186.193.22',443)
tcp_client_socket.connect(addr_server)
# 拼接请求协议
# 请求行
request_line = 'GET / HTTP/1.1\r\n'
# 请求头
request_header = 'Host:wow.blizzard.cn\r\n'
# 空行
request_blank = '\r\n'
# 拼接报文
request_data = request_line + request_header + request_blank
# 发送请求协议
tcp_client_socket.send(request_data.encode())
# 接收服务器的响应内容
recv_data = tcp_client_socket.recv(102400)
recv_text = recv_data.decode()
# print(recv_data)
print(recv_text)
# 保存数据
response_body_index = recv_text.find('\r\n\r\n')
# print(response_body_index)
response_body = recv_text[response_body_index + 4:]
print(response_body)
with open('response_data.html','w') as f:
f.write(response_body)
# 关闭连接
tcp_client_socket.close()