32 lines
619 B
Python
Raw Normal View History

2023-07-21 16:51:32 +08:00
import socket
# 创建一个socket对象
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 获取服务器的IP地址
2023-07-21 20:03:30 +08:00
server_ip = socket.gethostbyname('baidu.com')
2023-07-21 16:51:32 +08:00
server_port = 80
# 连接到服务器
s.connect((server_ip, server_port))
# 创建HTTP GET请求
2023-07-21 20:03:30 +08:00
request = 'GET / HTTP/1.1\r\nHost: baidu.com\r\n\r\n'
# print('request:', request)
2023-07-21 16:51:32 +08:00
s.send(request.encode())
# 接收服务器的响应
response = ''
while True:
2023-07-21 20:03:30 +08:00
try:
recv = s.recv(1024)
except:
break
2023-07-21 16:51:32 +08:00
if not recv:
break
response += recv.decode()
s.close()
assert 'HTTP/1.1 200 OK' in response
print('PASS')